所有DTM值的每小时SQL计数

时间:2019-10-28 17:03:10

标签: sql oracle

我已有一个具有Oracle DTM格式的值的范围,例如 22-NOV-18 12.20.45.430838000

我的目标是每小时对所有值范围进行一次计数。我希望计算不同日期的所有唯一值。

事实证明这很困难,因为提取函数只能从需要多个值的字段中提取1个日期类型。

我能够编写以下查询,该查询提供了整个范围内每天的计数。

select trunc(created_dtm), count(*) CreatedCount from

(select ord.order_id, ordser.service_id, ord.product_type, ord.created_dtm ,ord.last_modified_dtm from SCADRON_OWNER.orders ord, SCADRON_OWNER.order_subtype os, SCADRON_OWNER.order_services ordser where ord.order_id = os.order_id and ordser.order_id = ord.order_id and os.subtype = 'FttcActiveLineTakeoverUpgrade' and ord.order_status
= 'Completed' )

group by trunc(created_dtm) order by trunc(created_dtm);

产生如下结果:

enter image description here

但是我想对此进行优化,以便每小时可以对所有值进行计数,有没有简便的方法?

1 个答案:

答案 0 :(得分:1)

我想你想要

select cast(trunc(created_dtm, 'hh') as timestamp),
       count(*)
from SCADRON_OWNER.orders ord join
     SCADRON_OWNER.order_subtype os
     on ord.order_id = os.order_id join
     SCADRON_OWNER.order_services ordser 
     on ordser.order_id = ord.order_id 
where os.subtype = 'FttcActiveLineTakeoverUpgrade' and 
      ord.order_status = 'Completed' 
group by cast(trunc(created_dtm, 'hh') as timestamp)
order by min(created_dtm);

这会将值转换为时间戳,以便您可以看到它。或者,将其转换为字符串:

select to_char(created_dtm, 'YYYY-MM-DD HH24') || ':00:00',
       count(*)
from SCADRON_OWNER.orders ord join
     SCADRON_OWNER.order_subtype os
     on ord.order_id = os.order_id join
     SCADRON_OWNER.order_services ordser 
     on ordser.order_id = ord.order_id 
where os.subtype = 'FttcActiveLineTakeoverUpgrade' and 
      ord.order_status = 'Completed' 
group by to_char(created_dtm, 'YYYY-MM-DD HH24')
order by min(created_dtm);