Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Selecting DISTINCT Expand / Collapse
Author
Message
Posted Thursday, May 16, 2013 6:50 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 21, 2015 4:30 AM
Points: 23, Visits: 94
I have three fields I am interested in:

Employee_ID,
Employee_Title,
Employee_Entry

I want to be able to select these three fields but only the record with the most recent Employee_Entry date. In some cases an Employee may have moved job titles and have several entries against the Employee_ID.

I have started with something like this:

SELECT DISTINCT
Employee_ID,
Employee_Title,
Employee_Entry

FROM emp.employees

I have tried using Max(Employee_Entry) but this returns all records for those who have multiple titles. I just want the current most recent Employee_title.
Post #1453485
Posted Thursday, May 16, 2013 6:54 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:05 AM
Points: 14,407, Visits: 37,679
i think using the row number with a sub select will get you what you are after:
SELECT
Employee_ID,
Employee_Title,
Employee_Entry
FROM (SELECT
ROW_NUMBER() OVER (partition BY Employee_ID ORDER BY Employee_Entry DESC) AS RW,
Employee_ID,
Employee_Title,
Employee_Entry
FROM emp.employees) myAlias
WHERE RW = 1




Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1453490
Posted Thursday, May 16, 2013 6:58 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 21, 2015 4:30 AM
Points: 23, Visits: 94
That did it.

Thank you.
Post #1453493
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse