2013-12-17 3 views
0

Итак, что я пытаюсь сделать в основном, подсчитывает количество отправлений, в час, за перевозчика, но у меня возникают трудности с его суммой в столбцах, которые я хочу. Цель состоит в том, чтобы вместо 1 или 2 часа 1 или 2 часа у него была бы сумма (общие отгрузки). По существу, 24 часа, чтобы мы могли видеть тенденции времени суток ...Oracle SQL Count Records за час

Код:

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; 

Выход:

enter image description here

Но, цель состоит в том, чтобы иметь грузов под час.

спасибо.

Вот новый подход, который я сделал. Но он показывает много нулей и хотел бы, чтобы он показывал только цифры.

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; 
+0

Ответ был найден по этой теме. http://stackoverflow.com/questions/20647329/oracle-sql-count-per-hour/20647392?noredirect=1#comment30911561_20647392 – Spartacus38

ответ

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'); 
+0

В основном, где поставки столбец с данными, я бы хотел, что под Hour1, а не в отдельной колонке, то же самое для каждого часа. Благодарю. – Spartacus38

+0

Тогда решение, которое я предложил, должно быть в порядке. У вас будет что-то вроде этого: Destination A | 01 | Отгрузки ..... Пункт назначения A | 02 | Отгрузки .... и т. Д. (До 24), тогда вы можете извлекать тенденции во внешнем приложении, если используете какой-либо ... – MiGro

+0

Но я не могу создавать таблицы на своем сервере, так как это RO. – Spartacus38

Смежные вопросы