Results 1 to 7 of 7
  1. #1
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72

    Help with Query

    Hello,

    I have a table Employees:
    ID
    Manager1
    Manager2
    Manager3
    EmployeeType (A,B)

    Manager1, Manager2, Manager3 are ID numbers of employees.

    I wish to retrieve for each employee how many subordinate he has by EmployeeType .



    I mean: for each ID:
    count how many records has Manager1=ID or Manager2=ID or Manager3=ID and their EmployeeType = A
    count how many records has Manager1=ID or Manager2=ID or Manager3=ID and their EmployeeType = B

    My desired output for example:
    ID #A #B
    1 10 5
    2 4 5

    Can you help me ?

    Thank you for your help

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    This is one way:
    TRANSFORM Count(Query1.[ID]) AS CountOfID
    SELECT Query1.[MgrID]
    FROM (SELECT ID, Nz([Manager1],Nz([Manager2],[Manager3])) AS MgrID, EmployeeType FROM Employees) As Query1
    GROUP BY Query1.[MgrID]
    PIVOT Query1.[EmployeeType];
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72
    Thank you for your help.
    I will try to implement it according to your suggestion.

    Thank you for your support

  4. #4
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72
    Hello,

    I did not success in getting the desired result with the above query.

    Is there any other way to implement it ?

    Thank you for your help

  5. #5
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72
    I tried to solve my problem with multiple queries
    but my backend is located on a server and the response time is not good.

    I understand that my goal can be achieved by cross tab but I dont know how to do it.

    My input:
    ID
    Manager1
    Manager2
    EmployeeType

    for example:
    ID=1 Manager1=Null Manager2=Null EmployeeType=A
    ID=2 Manager1=1 Manager2=Null EmployeeType=B
    ID=3 Manager1=2 Manager2=Null EmployeeType=B
    ID=4 Manager1=1 Manager2=2 EmployeeType=A

    Output:
    ID
    #Employees Type A
    #Employees Type B

    output:
    ID=1 #EmployeesTypeA=1 #EmployeesTypeB=1
    ID=2 #EmployeesTypeA=1 #EmployeesTypeB=1


    Thank you for your help

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I made an example of your table and called it SNOOPY2003, so you will have to change references in the query to your table name where appropriate.

    Make a query with this code:
    Code:
    SELECT Snoopy2003.Manager1 AS Manager, Snoopy2003.EmployeeType
    FROM Snoopy2003
    WHERE (not isnull(manager1))
    UNION SELECT Snoopy2003.Manager2 AS Manager, Snoopy2003.EmployeeType
    FROM Snoopy2003
    WHERE(not isnull(Manager2))
    Call this query Qry_CrosstabSetup

    Make a query with this code:
    Code:
    TRANSFORM Count(Qry_CrosstabSetup.Manager) AS CountOfManager
    SELECT Qry_CrosstabSetup.Manager
    FROM Qry_CrosstabSetup
    GROUP BY Qry_CrosstabSetup.Manager
    PIVOT Qry_CrosstabSetup.EmployeeType
    it should give you the results you seem to want.

  7. #7
    snoopy2003 is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Feb 2011
    Posts
    72
    Hello,

    Thank you very much. I appricaiate your help.
    This is what I was trying to implement.

    I had to add the keyword 'All' to the 'UNION' command
    in order to retrieve all the results from both queries.

    Your query will be very helpful for me and makes the
    calculation process simpler and faster.

    Thank you

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums