Click here to Skip to main content
16,021,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Get all the employees latest Document date in sql server 


No	Employee	DocumentDateNew
1	Employee1	3/22/2018
2	Employee1	5/22/2018
3	Employee1	10/23/2017
4	Employee2	3/21/2018
5	Employee2	6/22/2018
6	Employee2	10/23/2017
7	Employee2	10/12/2017
8	Employee3	3/22/2018
9	Employee3	11/10/2017



i need result like
NO	Employee	DocumentDateNew
1	Employee1	3/22/2018
4	Employee2	3/21/2018
8	Employee3	3/22/2018


What I have tried:

Get the record all the employee based on latest documents date
Posted
Updated 23-Mar-18 7:26am
v3

SQL
SELECT EMPLOYEE,MAX(DOCUMENTDATE) FROM EMPLOYEETABLE GROUP BY EMPLOYEE
 
Share this answer
 
Comments
DhananjayanP 23-Mar-18 1:33am    
thank you for your answer, but I am getting EMPLOYEE name as Duplicate
select Employee, max(DocumentDateNew) as DocumentDateNew from emptable group by Employee
 
Share this answer
 
Comments
DhananjayanP 23-Mar-18 2:01am    
thank you for your answer, but I am getting EMPLOYEE name as Duplicate
itsmypassion 23-Mar-18 2:28am    
Do not include Column "No" in Group by List.
If you need to include the "No" column:
SQL
WITH cte As
(
    SELECT
        No,
        Employee,
        DocumentDateNew,
        ROW_NUMBER() OVER (PARTITION BY Employee ORDER BY DocumentDateNew DESC) As RN
    FROM
        YourTable
)
SELECT
    No,
    Employee,
    DocumentDateNew
FROM
    cte
WHERE
    RN = 1
;

Demo[^]
ROW_NUMBER (Transact-SQL) | Microsoft Docs[^]

NB: Your expected output is wrong. Taking Employee1 as an example, 22nd May 2018 is later than 22nd March 2018, so the output should return row 2, not row 1.
 
Share this answer
 
Comments
Maciej Los 23-Mar-18 16:13pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900