Oracle如何选择一组最新条目

时间:2014-10-23 17:24:56

标签: sql oracle datetime select unique

我有一个如下所示的Oracle表:

URL   SIZE   MODDATE     MODTIME
AAA   100    24-OCT-14   10:00:00
AAA   100    24-OCT-14   09:00:00
AAA   100    23-OCT-14   08:00:00
BBB   100    24-OCT-14   10:00:00
BBB   100    24-OCT-14   09:00:00
BBB   100    23-OCT-14   08:00:00
CCC   100    24-OCT-14   10:00:00
CCC   100    24-OCT-14   09:00:00
CCC   100    23-OCT-14   08:00:00

我需要根据MODDATE& amp;获取每行的最新数据。 MODTIME。所以结果看起来像这样:

URL   SIZE   MODDATE     MODTIME
AAA   100    24-OCT-14   10:00:00
BBB   100    24-OCT-14   10:00:00
CCC   100    24-OCT-14   10:00:00

SELECT如何看起来像(Oracle)?

谢谢你的帮助!

3 个答案:

答案 0 :(得分:0)

尝试:

SELECT * 
FROM table
WHERE MODDATE IN (SELECT max(MODDATE) FROM table)
ORDER BY MODTIME

答案 1 :(得分:0)

您可以使用ROW_NUMBER分析函数

SELECT "URL", "SIZE", "MODDATE", "MODTIME"
FROM
(
SELECT "URL", "SIZE", "MODDATE", "MODTIME",
       ROW_NUMBER() OVER ( PARTITION BY "URL" order by "MODDATE" desc, "MODTIME" desc) as seq
FROM Table1
) T
Where T.seq =1

答案 2 :(得分:0)

假设列MODDATE是DATE数据类型而MODTIME是字符串。

select DISTINCT URL, SIZE,
   LAST_VALUE(MODDATE) OVER (PARTITION BY URL, SIZE ORDER BY MODDATE, MODTIME) as MODDATE,
   LAST_VALUE(MODTIME) OVER (PARTITION BY URL, SIZE ORDER BY MODDATE, MODTIME) as MODTIME
from your_table;