我有一个查询,显示每个运营商每小时的发货量。我按小时计算它,但是在上一小时完成之前它显示为零,然后报告下一小时的数据。基本上,想要从左到右阅读,如果运营商在那个小时内没有发货,则为NULL。
代码:
select router_destination_code,
count(case when to_char(app_last_updated_date_utc, 'HH24') = '00' then router_destination_code else NULL end) as "Hour 1",
count(case when to_char(app_last_updated_date_utc, 'HH24') = '01' then router_destination_code else NULL end) as "Hour 2",
count(case when to_char(app_last_updated_date_utc, 'HH24') = '02' then router_destination_code else NULL end) as "Hour 3",
count(case when to_char(app_last_updated_date_utc, 'HH24') = '03' then router_destination_code else NULL end) as "Hour 4",
count(case when to_char(app_last_updated_date_utc, 'HH24') = '04' then router_destination_code else NULL end) as "Hour 5",
count(case when to_char(app_last_updated_date_utc, 'HH24') = '05' then router_destination_code else NULL end) as "Hour 6"
from booker.routing_container_history
where
app_last_updated_by_module in ('ManualSlam', 'slam')
and app_last_updated_date_utc between 'dec/07/2013 00:00:00' and 'dec/14/2013 00:00:00'
group by
router_destination_code,
case when to_char(app_last_updated_date_utc, 'HH24') = '00' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '01' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '02' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '03' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '04' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '05' then router_destination_code else NULL end
order by
case when to_char(app_last_updated_date_utc, 'HH24') = '00' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '01' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '02' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '03' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '04' then router_destination_code else NULL end,
case when to_char(app_last_updated_date_utc, 'HH24') = '05' then router_destination_code else NULL end,
count(Router_Destination_code) desc;
输出:
来自GORDON LINOFF的新查询
select router_destination_code,
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '00' then 1 end) as "Hour 1",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '01' then 2 end) as "Hour 2",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '02' then 3 end) as "Hour 3",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '03' then 4 end) as "Hour 4",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '04' then 5 end) as "Hour 5",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '05' then 6 end) as "Hour 6"
from booker.routing_container_history
where
app_last_updated_by_module in ('ManualSlam', 'slam')
and app_last_updated_date_utc between 'dec/07/2013 00:00:00' and 'dec/14/2013 00:00:00'
group by
router_destination_code,
case when to_char(app_last_updated_date_utc, 'HH24') = '00' then 1 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '01' then 2 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '02' then 3 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '03' then 4 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '04' then 5 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '05' then 6 end
order by
case when to_char(app_last_updated_date_utc, 'HH24') = '00' then 1 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '01' then 2 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '02' then 3 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '03' then 4 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '04' then 5 end,
case when to_char(app_last_updated_date_utc, 'HH24') = '05' then 6 end,
count(Router_Destination_code) desc;
答案 0 :(得分:1)
而不是使用count(. . .)
使用sum(. . .)
,而不是:
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '00'
then 1 end) as "Hour 1"
编辑:
为了清楚起见,查询应该是:
select router_destination_code,
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '00' then 1 end) as "Hour 1",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '01' then 1 end) as "Hour 2",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '02' then 1 end) as "Hour 3",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '03' then 1 end) as "Hour 4",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '04' then 1 end) as "Hour 5",
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '05' then 1 end) as "Hour 6"
from booker.routing_container_history
where app_last_updated_by_module in ('ManualSlam', 'slam') and
app_last_updated_date_utc between 'dec/07/2013 00:00:00' and 'dec/14/2013 00:00:00'
group by router_destination_code
order by sum(case when to_char(app_last_updated_date_utc, 'HH24') = '00' then 1 end),
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '01' then 1 end),
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '02' then 1 end),
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '03' then 1 end),
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '04' then 1 end),
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '05' then 1 end),
count(Router_Destination_code) desc;