每小时Oracle SQL计数记录

时间:2013-12-17 21:02:54

标签: sql oracle count sum

所以,我基本上要做的就是计算每个运营商每小时的出货量,但是我很难在我想要的列中加总。目标是在小时1或小时2下不是1或2,而是总和(总发货量)。基本上24小时,所以我们可以看到一天中的时间趋势...

代码:

    select router_destination_code, 
case when to_char(app_last_updated_date_utc, 'HH24') = '00' then '1' else NULL end as "Hour 1",
case when to_char(app_last_updated_date_utc, 'HH24') = '01' then '2' else NULL end as "Hour 2",
case when to_char(app_last_updated_date_utc, 'HH24') = '02' then '3' else NULL end as "Hour 3",
--case when app_last_updated_date_utc between 'dec/07/2013 16:00:00' and 'dec/14/2013 17:00:00' then count(Router_Destination_code) else NULL end as "Hour_1"
count(Router_Destination_code) as Shipments
from booker.routing_container_history
where 
app_last_updated_by_module in ('ManualSlam', 'slam')
and app_last_updated_date_utc between 'dec/07/2013 16:00:00' and 'dec/14/2013 16:00:00'
group by 
router_destination_code, 
case when to_char(app_last_updated_date_utc, 'HH24') = '00' then '1' else NULL end, 
case when to_char(app_last_updated_date_utc, 'HH24') = '01' then '2' else NULL end, 
case when to_char(app_last_updated_date_utc, 'HH24') = '02' then '3' else NULL end
order by 
case when to_char(app_last_updated_date_utc, 'HH24') = '00' then '1' else NULL end, 
case when to_char(app_last_updated_date_utc, 'HH24') = '01' then '2' else NULL end, 
case when to_char(app_last_updated_date_utc, 'HH24') = '02' then '3' else NULL end, 
count(Router_Destination_code) desc;

输出:

enter image description here

但是,目标是将货物放在一小时之内。

谢谢。

这是我做的一种新方法..但是它显示了很多零,并希望它只显示数字。

select router_destination_code, 
count(case when to_char(app_last_updated_date_utc, 'HH24') = '16' then router_destination_code else NULL end) as "Hour 1",
count(case when to_char(app_last_updated_date_utc, 'HH24') = '17' then router_destination_code else NULL end) as "Hour 2"
--case when app_last_updated_date_utc between 'dec/07/2013 16:00:00' and 'dec/14/2013 17:00:00' then count(Router_Destination_code) else NULL end as "Hour_1"
--count(Router_Destination_code) as Shipments
from booker.routing_container_history
where 
app_last_updated_by_module in ('ManualSlam', 'slam')
and app_last_updated_date_utc between 'dec/07/2013 16:00:00' and 'dec/14/2013 16:00:00'
group by 
router_destination_code, 
case when to_char(app_last_updated_date_utc, 'HH24') = '16' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '17' then router_destination_code else NULL end
order by 
case when to_char(app_last_updated_date_utc, 'HH24') = '16' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '17' then router_destination_code else NULL end,
count(Router_Destination_code) desc;

1 个答案:

答案 0 :(得分:1)

我不知道我是否理解你,但你可能会尝试从日期中提取小时,然后分组:

检查这个小提琴(抱歉语法错误): http://sqlfiddle.com/#!4/a0a97/6

create table a (id number, data date);
insert into a (id, data)
values (1, to_date( '2013-01-01 13:12:01', 'yyyy-mm-dd hh24:mi:ss'));
insert into a (id, data)
values (1, to_date( '2013-01-01 13:12:01', 'yyyy-mm-dd hh24:mi:ss'));
insert into a (id, data)
values (1, to_date( '2013-01-01 14:12:01', 'yyyy-mm-dd hh24:mi:ss'));

select to_char(data,'HH24'), count(1) from a group by to_char(data,'HH24');