最近4天每15分钟的计数(按列)

时间:2019-03-14 11:06:51

标签: sql pivot case

我有这个查询,每15分钟就会给我输出多少个“否”。

select trunc(rcv_dt,'hh24') + (trunc(to_char(rcv_dt,'mi')/15)*15)/24/60 as timeline, count(distinct(NO)) as count
from tbl where trunc(rcv_dt) = trunc(SYSDATE -1)
group by  trunc(rcv_dt,'hh24') + (trunc(to_char(rcv_dt,'mi')/15)*15)/24/60
order by trunc(rcv_dt,'hh24') + (trunc(to_char(rcv_dt,'mi')/15)*15)/24/60

输出:

enter image description here

但是最近几天我需要以下输出格式的类似数据。在这方面需要您的帮助。

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为这可以满足您的要求

select (trunc(to_char(rcv_dt, 'mi')/15)*15)/24/60 as timeline, 
       count(distinct case when trunc(rcv_dt,'hh24') = date '2019-03-13' then NO end) as no_20190313,
       count(distinct case when trunc(rcv_dt,'hh24') = date '2019-03-12' then NO end) as no_20190312,
       count(distinct case when trunc(rcv_dt,'hh24') = date '2019-03-11' then NO end) as no_20190311
from tbl
where trunc(rcv_dt) >= date '2019-03-11'
group by (trunc(to_char(rcv_dt,'mi')/15)*15)/24/60
order by (trunc(to_char(rcv_dt,'mi')/15)*15)/24/60;
相关问题