How can i get first value of each hours? ORACLE

不想你离开。 提交于 2021-01-28 06:29:32

问题


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

How can i get first value of each hours in oracle?

like that:

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

回答1:


If you want the first value, then you can use row_number() or 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;



回答2:


GROUp BY date in hours

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

Or using TRUNC

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


来源:https://stackoverflow.com/questions/29230782/how-can-i-get-first-value-of-each-hours-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!