问题
I have a query that shows the amount of shipments per hour, per carrier. I have it cased by hour, but it is showing zero's until the previous hour is complete before reporting data for next hour. Essentially, would like to read left to right, and have NULL if carrier didn't have shipment during that hour.
Code:
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;
Output:
NEW QUERY FROM 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;
回答1:
Instead of using count(. . .) use sum(. . .), as in:
sum(case when to_char(app_last_updated_date_utc, 'HH24') = '00'
then 1 end) as "Hour 1"
EDIT:
Just to be clear, the query should be:
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;
来源:https://stackoverflow.com/questions/20647329/oracle-sql-count-per-hour