我怎样才能获得每小时的第一价值? ORACLE

时间:2015-03-24 10:57:21

标签: sql oracle oracle-sqldeveloper

id           date        value
1   08.03.2015 20:01:47 1383322
2   08.03.2015 20:14:01 1383382
3   08.03.2015 20:19:17 1383412
4   08.03.2015 20:22:45 1383441
5   08.03.2015 20:35:55 1383551
6   08.03.2015 20:42:43 1383604
7   08.03.2015 20:44:56 1383630
8   08.03.2015 21:08:17 1383880
9   08.03.2015 21:16:36 1383959
10  08.03.2015 22:37:48 1384613
11  08.03.2015 22:40:12 1384642
12  08.03.2015 22:42:25 1384667
13  08.03.2015 22:44:38 1384696
14  08.03.2015 22:46:51 1384721
15  08.03.2015 22:49:58 1384752
16  08.03.2015 22:52:17 1384775
17  08.03.2015 22:54:50 1384804

如何在oracle中获得每小时的第一个值?

像那样:

id          date         value
1   08.03.2015 20:01:47 1383322
8   08.03.2015 21:08:17 1383880
10  08.03.2015 22:37:48 1384613

2 个答案:

答案 0 :(得分:2)

如果您想要第一个值,则可以使用row_number()keep

select t.*
from (select t.*,
             row_number() over (partition by to_char(date, 'YYYY-MM-DD HH24'),
                                order by date) as seqnum
      from table t
     ) t
where seqnum = 1;

答案 1 :(得分:0)

以小时为单位的组合日期

SELECT date, MIN(value)
FROM table
GROUP BY TO_CHAR(date,'HH24');

或使用TRUNC

SELECT date, MIN(value)
FROM table
GROUP BY TRUNC(date, 'HH24');