如何使用Oracle sql从下表中提取具有最新结束日期的唯一名称。
NAME Start DATE End Date
ATO210291676 21/11/2015 18/05/2016
ATO210291676 19/05/2016 30/06/2017
ATO210291889 21/11/2015 18/05/2016
ATO210291889 19/05/2016 30/06/2017
ATO210291923 21/11/2015 18/05/2016
ATO210291923 19/05/2016 30/06/2017
ATO210186276 25/05/2015 24/05/2016
ATO210186276 25/05/2016 30/06/2017
ATO210186373 25/05/2015 24/05/2016
ATO210186373 25/05/2016 30/06/2017
TNT212498119 29/06/2015 28/06/2016
TNT212498119 29/06/2016 30/06/2017
TNT212498349 26/02/2016 30/06/2017
ATO212491256 26/02/2016 30/06/2017
ATO212502235 26/02/2016 30/06/2017
ATO212522556 26/02/2016 30/06/2017
ATO212522582 26/02/2016 30/06/2017
CVE114AK0076 16/09/2015 26/06/2016
CVE114AK0076 27/06/2016 30/06/2017
CVE414AK0012 15/09/2015 26/06/2016
CVE414AK0012 27/06/2016 30/06/2017
我试图产生以下结果。
ATO210291676 19/05/2016 30/06/2017
ATO210291889 19/05/2016 30/06/2017
ATO210291923 19/05/2016 30/06/2017
ATO210186276 25/05/2016 30/06/2017
ATO210186373 25/05/2016 30/06/2017
TNT212498119 29/06/2016 30/06/2017
TNT212498349 26/02/2016 30/06/2017
ATO212491256 26/02/2016 30/06/2017
ATO212502235 26/02/2016 30/06/2017
ATO212522556 26/02/2016 30/06/2017
ATO212522582 26/02/2016 30/06/2017
CVE114AK0076 27/06/2016 30/06/2017
CVE414AK0012 27/06/2016 30/06/2017
结果集应该如上所述
答案 0 :(得分:0)
SELECT NAME, MAX(ENDDate)
from tab
GROUP BY NAME
您可以使用max
功能获取最新日期。
答案 1 :(得分:0)
SELECT Name, StartDate, EndDate
FROM [Your_Table] A
INNER JOIN
(
SELECT Name, MAX(EndDate) AS MaxEndDate
FROM [YOUR_TABLE]
GROUP BY Name
) B ON A.Name = B.Name AND A.EndDate = B.EndDate
答案 2 :(得分:0)
select NAME
,"Start DATE"
,"End Date"
from (select t.*
,row_number () over (partition by NAME order by "End Date" desc) as rn
from t
) t
where rn = 1
;
答案 3 :(得分:0)
您可以在一次传递中获得所需的结果,而无需连接或子查询。通过在实际数据上尝试不同的解决方案,您是否可以确定这是否可以提高速度。
select name,
max(start_date) keep (dense_rank last order by end_date) as start_date,
max(end_date) as end_date
from table_name
group by name
;