統計t_tjsj表中按照 cjsj (date類型)每5分鐘統計0點到1點這個一小時內時間段內的數量:
select lpad(floor(to_char(cjsj,'mi')/5)*5,2,0) cjsj, count(bh) num
from t_tjsj where cjsj>to_date('2018-04-15 00:00:00','yyyy-MM-dd hh24:mi:ss')
and cjsj<=to_date('2018-04-15 01:00:00','yyyy-MM-dd hh24:mi:ss')
group by lpad(floor(to_char(cjsj,'mi')/5)*5,2,0)
order by lpad(floor(to_char(cjsj,'mi')/5)*5,2,0) ;
統計預期結果:
cjsj | num |
05 | 111 |
10 | 232 |
15 | 211 |
20 | 12 |
25 | 3214 |
30 | 34 |
35 | 234 |
40 | 454 |
45 | 674 |
50 | 86 |
55 | 35 |
統計t_tjsj表中按照 cjsj (date類型)統計0點到24點一天內每5分鐘時間段內的數量:
select to_char(cjsj,'hh24')||':'||lpad(floor(to_char(cjsj,'mi')/5)*5,2,0) cjsj, count(bh) num
from t_tjsj where cjsj>to_date('2018-04-15 00:00:00','yyyy-MM-dd hh24:mi:ss')
and cjsj<=to_date('2018-04-15 01:00:00','yyyy-MM-dd hh24:mi:ss')
group by to_char(cjsj,'hh24')||':'||lpad(floor(to_char(cjsj,'mi')/5)*5,2,0)
order by to_char(cjsj,'hh24')||':'||lpad(floor(to_char(cjsj,'mi')/5)*5,2,0) ;
重點函數:
to_char(cjsj,'hh24') ----時間取小時
lpad(floor(to_char(cjsj,'mi')/5)*5,2,0) --時間取對應的五分鐘段