我的表格包含以下数据:
LabId lab_name Measured_date value
------ -------- ------------- -----
1519253 LAST_DBP 19-JUN-12 78
1519252 LAST_SBP 19-JUN-12 151
1519267 LDL 27-AUG-12 84
1519262 LDL 17-AUG-12 86
1519250 SBP 17-AUG-12 181
我想从这个表中检索数据,这样,如果同一个lab_name存在多个记录,我需要记录最新的measured_date。所以我想要的输出应该是这样的:
LabId lab_name Measured_date value
------ -------- ------------- -----
1519253 LAST_DBP 19-JUN-12 78
1519252 LAST_SBP 19-JUN-12 151
1519267 LDL 27-AUG-12 84
1519250 SBP 17-AUG-12 181
由于 戈皮
答案 0 :(得分:0)
使用Analytic fn:Rank()
WITH TEMP AS
(
SELECT LabId , lab_name ,Measured_date ,value,
rank() OVER (PARTITION BY lab_name ORDER BY Measured_date desc) AS RK
FROM TABLE1
)
SELECT LabId , lab_name ,Measured_date ,value FROM TEMP WHERE RK=1;