Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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: Tuesday, March 18, 2014 6:31 AM
Points: 23, Visits: 93
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: Yesterday @ 2:35 PM
Points: 12,962, Visits: 32,498
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1453490
Posted Thursday, May 16, 2013 6:58 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 6:31 AM
Points: 23, Visits: 93
That did it.

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

Add to briefcase

Permissions Expand / Collapse