簡體   English   中英

每小時Oracle SQL計數記錄

[英]Oracle SQL Count Records per Hour

因此,我基本上想做的是計算每個承運商每小時的運輸量,但是我很難在我想要的列中求和。 目標是在總時長(總出貨量)中而不是在小時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;

我不知道我是否了解您,但是您可以嘗試從日期中提取小時數,然后進行分組:

檢查此小提琴(對不起,語法錯誤): 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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM