Oracle SQL count() 每小时
Posted
技术标签:
【中文标题】Oracle SQL count() 每小时【英文标题】:Oracle SQL count() per hour 【发布时间】:2013-12-18 00:15:12 【问题描述】:我有一个查询,显示每个承运人每小时的发货量。我按小时计算,但在报告下一小时的数据之前,它会显示零,直到前一小时完成。本质上,希望从左到右阅读,如果承运人在那一小时内没有发货,则为 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;
输出:
戈登·林诺夫提出的新问题
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;
【问题讨论】:
请提供样本输入和期望的结果。你的问题不清楚。 @GordonLinoff 我在下面回复了您的回答,一切似乎都很好!我在下面回复。 【参考方案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;
【讨论】:
你在哪里有 1,我不想再 'router_destination_code' 吗?做总和?基本上我想要 HH24 = 01 时 router_destination_code 的总和,等等 02,03 等。谢谢。 我使用了你的建议,但是,现在它不是 0,而是 NULL..Updated question with new query。 会在那个时间范围内对所有的数据求和,还是取平均值?谢谢。 @Spartacus38 。 . .它将在时间范围内汇总,就像您的原始查询在时间范围内计算所有内容一样。 @Spartacus38 。 . .您可以将else 0
添加到每个case
语句中。以上是关于Oracle SQL count() 每小时的主要内容,如果未能解决你的问题,请参考以下文章
Oracle SQL to_date & to_timestamp ORA-01858: 在需要数字的地方发现了一个非数字字符 & ORA-01850: 小时必须在 0 到 23 之间