;WITH Latest
AS
(
SELECT *,
RN = ROW_NUMBER() OVER (PARTITION BY employee_name ORDER BY [Date] DESC)
FROM TABLE_NAME
)
SELECT employee_name
,Dept_Code
,Supervisor
,[Date]
FROM Latest
WHERE RN = 1
How do I filter out dulplicate rows in my SQL query
-
19-10-2022 - |
题
My initial query (select * from tbl order by employee_id) returns the following:
employee_name Dept_Code Supervisor Date
John 99213 Mike 2013-01-10
John 99213 Dan 2013-12-13
John 99214 Kelly 2013-12-10
John 99215 Tammy 2013-09-09
Nancy 99244 Roy 2013-05-07
Nancy 99213 Cory 2013-04-05
Nancy 99214 Donald 2013-04-19
Nancy 99243 William 2013-07-01
Nancy 99244 Nick 2013-06-03
Rich 99203 Amber 2013-04-12
Dawn 99213 Nelson 2013-11-06
Rob 99213 Henrey 2013-08-01
Lewis 99213 Eric 2013-12-02
Alex 99203 Chris 2013-08-08
Alex 99243 Frank 2013-09-25
Bing 67228 Paula 2013-12-17
Bing 92225 Paula 2013-11-19
I only want to return the employee_name, Dept_Code and Supervisor with the max(Date). However, when I run the below. My result are the same.
SELECT employee_namd ,
Dept_Code ,
Supervisor ,
MAX(Date) AS 'Date'
FROM tbl
GROUP BY employee_name ,
Dept_Code ,
Supervisor
ORDER BY employee_name
I would like to see only:
employee_name Dept_Code Supervisor Date
John 99213 Dan 2013-12-13
Nancy 99243 William 2013-07-01
Rich 99203 Amber 2013-04-12
Dawn 99213 Nelson 2013-11-06
Rob 99213 Henrey 2013-08-01
Lewis 99213 Eric 2013-12-02
Alex 99243 Frank 2013-09-25
Bing 67228 Paula 2013-12-17
没有正确的解决方案
其他提示
不隶属于 StackOverflow