【问题标题】:Oracle SQL Count Records per Hour每小时 Oracle SQL 计数记录
【发布时间】:2013-12-17 21:02:54
【问题描述】:

所以,我要做的基本上是计算每个承运人每小时的发货量,但我很难将其汇总到我想要的列中。目标是在第 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;

输出:

但是,我们的目标是在小时内发货。

谢谢。

这是我采用的一种新方法。但它显示了很多零,并且希望它只显示整个数字。

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;

【问题讨论】:

标签: sql oracle count sum


【解决方案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');

【讨论】:

  • 基本上,列出货量与数据一起,我希望在 Hour1 下方,而不是在单独的列中,然后每小时相同。谢谢。
  • 那么我提出的解决方案就OK了。你会得到这样的东西: 目的地 A | 01 |发货.....目的地A | 02 | Shipments.... 等等(到 24)然后您可以在外部应用程序中提取趋势,如果您使用任何...
  • 但是,我无法在我的服务器上创建表,因为它是 RO。
猜你喜欢
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多