Link to home
Start Free TrialLog in
Avatar of Member_2_1242703
Member_2_1242703

asked on

Querying data from 3 SQL tables

With this query:

SELECT e.FIRST_NAME, e.LAST_NAME, e.REHIRE_DT, e.JOBCODE, SUM(r.HOURS) AS SUM_HOURS
  FROM EMPLOYEES e
  JOIN REQUEST r ON r.EMPLID = e.EMPLID
 WHERE e.EMPLID IN (SELECT EMPLID FROM USER_ACCOUNTS WHERE MGRID = '5555')
 GROUP BY e.FIRST_NAME, e.LAST_NAME, e.REHIRE_DT, e.JOBCODE

Open in new window


I'm getting:

PETER GRIFFIN        1/1/2000  09876    40
PHILLIP FRY             4/4/2010  54321     62  
ZACK MORRIS         7/7/2015  12345     40
BRANDON WALSH 9/9/2016  67890     31

But now I need to get the following results (I'm essentially just adding the column 'ID' from USER_ACCOUNTS:)

PETER GRIFFIN        1/1/2000  09876    40     207945
PHILLIP FRY             4/4/2010  54321     62    436523
ZACK MORRIS         7/7/2015  12345     40    968126
BRANDON WALSH 9/9/2016  67890     31    003217

From the following 3 tables:

REQUEST
EMPLID HOURS
0001         40
0002         35
0002         12
0002         15
0003         20
0003         20
0004         10
0004         10
0004         11

USER_ACCOUNTS
ID             EMPLID    MGRID
207945       0001        5555
436523      0002        5555
968126      0003        5555
003217      0004        5555

EMPLOYEES
EMPLID    REHIRE_DT FIRST_NAME LAST_NAME JOBCODE
0001           1/1/2000       PETER          GRIFFIN        09876
0002           4/4/2010      PHILLIP            FRY            54321
0003           7/7/2015        ZACK           MORRIS       12345
0004           9/9/2016   BRANDON       WALSH        67890

WHERE USER_ACCOUNTS.MGRID = '5555'

How do I do this?
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_1242703
Member_2_1242703

ASKER

Well done! Thanks