How to get managers list

Posted on 2014-08-18
Last Modified: 2014-08-18
Thank you in advance for any feedback you may provide.

On the adventureworks database provided by MS Office they have on the humanresources.employee a field named organizationnode.  Based on this field I would
like to get a list of all managers only.  Please present me with the code I'm not too familiar with sql.
Question by:rborda
    LVL 23

    Expert Comment

    Select * from humanresources.employee
    where organizationnode like '%managers%'

    Were the humanresources.employee is the name of the table, organizationnode the name of the row and '%managers%' the phrase you are looking for in that row. the % are SQL wildcards.
    LVL 32

    Accepted Solution

    The OrganizationNode column is a hierarchyid. It's a compact data type to store hierarchy information in form of a path. This can be verifyed by using the ToString() method. btw, this data type is a CLR data type, thus ToString() is known method from classes in the .Net framework:

    SELECT  E.BusinessEntityID ,
            E.NationalIDNumber ,
            E.OrganizationNode ,
            E.OrganizationLevel ,
            E.JobTitle ,
    FROM    HumanResources.Employee E;

    Open in new window

    So now the question is: How do you define "manager" in conjunction to this hierarchy? Any none leaf? The necessary information is already present in the computed column OrganizationLevel which is OrganizationNode.GetLevel():

    SELECT  E.BusinessEntityID ,
            E.NationalIDNumber ,
            E.OrganizationNode ,
            E.OrganizationLevel ,
    FROM    HumanResources.Employee E
    WHERE   E.OrganizationLevel < ( SELECT  MAX(L.OrganizationLevel)
                                    FROM    HumanResources.Employee L

    Open in new window

    LVL 68

    Expert Comment

    This certainly sounds like some type of classwork or homework.  If so, we can/should only provide general approaches/ideas, not full code.

    Author Closing Comment

    It returned more than the number of supervisors, but it put me on the right track.  Thank you.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now