mikeewalton
asked on
SQL Query, return newest entry per Empl ID in query that returns several results per EMPL ID
I have the following query:
SELECT DELTEK.EMPL_LAB_INFO.EMPL_ ID, DELTEK.EMPL.LAST_FIRST_NAM E, DELTEK.EMPL_LAB_INFO.EMPL_ CLASS_CD, DELTEK.EMPL_LAB_INFO.ORG_I D,
DELTEK.EMPL_LAB_INFO.EFFEC T_DT, DELTEK.EMPL_LAB_INFO.S_HRL Y_SAL_CD
FROM DELTEK.EMPL_LAB_INFO INNER JOIN
DELTEK.EMPL ON DELTEK.EMPL_LAB_INFO.EMPL_ ID = DELTEK.EMPL.EMPL_ID
WHERE (DELTEK.EMPL.S_EMPL_STATUS _CD = 'ACT') AND (DELTEK.EMPL.TERM_DT IS NULL)
Returns:
EMPL_ID LAST_FIRST_NAME EMPL_CLASS_CD ORG_ID EFFECT_DT S_HRLY_SAL_CD
1001 MARK CORE-SLRY 1.1.01 11/16/2009 0:00 S
1001 MARK CORE-SLRY 1.1.01 1/25/2004 0:00 S
1001 MARK PARTNER 1.1.01 1/1/2000 0:00 S
1002 JIM CORE-SLRY 1.1.01 2/21/2005 0:00 S
1002 JIM PARTNER 1.1.01 1/1/2000 0:00 S
1002 JIM CORE-SLRY 1.1.01 1/25/2004 0:00 S
1002 JIM CORE-SLRY 1.1.01 11/16/2009 0:00 S
1003 DENNIS CORE-SLRY 1.1.01 11/16/2009 0:00 S
1003 DENNIS CORE-SLRY 1.1.01 1/25/2004 0:00 S
I need it to only return on record per EID, and that should be the record with the newest effective date.
i.e.
EMPL_ID LAST_FIRST_NAME EMPL_CLASS_CD ORG_ID EFFECT_DT S_HRLY_SAL_CD
1001 MARK CORE-SLRY 1.1.01 11/16/2009 0:00 S
1002 JIM CORE-SLRY 1.1.01 2/21/2005 0:00 S
1003 DENNIS CORE-SLRY 1.1.01 11/16/2009 0:00 S
Thanks
SELECT DELTEK.EMPL_LAB_INFO.EMPL_
DELTEK.EMPL_LAB_INFO.EFFEC
FROM DELTEK.EMPL_LAB_INFO INNER JOIN
DELTEK.EMPL ON DELTEK.EMPL_LAB_INFO.EMPL_
WHERE (DELTEK.EMPL.S_EMPL_STATUS
Returns:
EMPL_ID LAST_FIRST_NAME EMPL_CLASS_CD ORG_ID EFFECT_DT S_HRLY_SAL_CD
1001 MARK CORE-SLRY 1.1.01 11/16/2009 0:00 S
1001 MARK CORE-SLRY 1.1.01 1/25/2004 0:00 S
1001 MARK PARTNER 1.1.01 1/1/2000 0:00 S
1002 JIM CORE-SLRY 1.1.01 2/21/2005 0:00 S
1002 JIM PARTNER 1.1.01 1/1/2000 0:00 S
1002 JIM CORE-SLRY 1.1.01 1/25/2004 0:00 S
1002 JIM CORE-SLRY 1.1.01 11/16/2009 0:00 S
1003 DENNIS CORE-SLRY 1.1.01 11/16/2009 0:00 S
1003 DENNIS CORE-SLRY 1.1.01 1/25/2004 0:00 S
I need it to only return on record per EID, and that should be the record with the newest effective date.
i.e.
EMPL_ID LAST_FIRST_NAME EMPL_CLASS_CD ORG_ID EFFECT_DT S_HRLY_SAL_CD
1001 MARK CORE-SLRY 1.1.01 11/16/2009 0:00 S
1002 JIM CORE-SLRY 1.1.01 2/21/2005 0:00 S
1003 DENNIS CORE-SLRY 1.1.01 11/16/2009 0:00 S
Thanks
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
please check this article out to see your options:
https://www.experts-exchange.com/A_3203.html
https://www.experts-exchange.com/A_3203.html
ASKER
lluden, your query is very close, except it is returning the oldest entry for each instead of the newest.
ASKER
lluden, added: DESC to the order by in
:
ROW_NUMBER() OVER (PARTITION BY DELTEK.EMPL_LAB_INFO.EMPL_ ID ORDER BY DELTEK.EMPL_LAB_INFO.EFFEC T_DT DESC) AS Row
and it seems to be working, let me just double check it real quick.
:
ROW_NUMBER() OVER (PARTITION BY DELTEK.EMPL_LAB_INFO.EMPL_
and it seems to be working, let me just double check it real quick.
SELECT DELTEK.EMPL_LAB_INFO.EMPL_
MAX( DELTEK.EMPL_LAB_INFO.EFFEC
FROM DELTEK.EMPL_LAB_INFO INNER JOIN
DELTEK.EMPL ON DELTEK.EMPL_LAB_INFO.EMPL_
WHERE (DELTEK.EMPL.S_EMPL_STATUS
group by DELTEK.EMPL_LAB_INFO.EMPL_