SQL 2005: Select top N, group by ID with joins

Posted by Suzy Fresh on Stack Overflow See other posts from Stack Overflow or by Suzy Fresh
Published on 2010-04-25T19:13:28Z Indexed on 2010/04/25 20:03 UTC
Read the original article Hit count: 276

Filed under:
|
|

I'm having real difficulty with a query involving 3 tables. I need to get the 3 newest users per department grouped by department names. The groups should be sorted by the users.dateadded so the department with the newest activity is first. The users can exist in multiple departments so Im using a lookup table that just contains the userID and deptID. My tables are as follows.

Department - depID|name

Users - userID|name|dateadded

DepUsers - depID|userID

The output I need would be

Receiving
John Doe - 4/23/2010
Bill Smith - 4/22/2010

Accounting
Steve Jones - 4/22/2010
John Doe - 4/21/2010

Auditing
Steve Jones - 4/21/2010
Bill Smith - 4/21/2010

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2005