计算oracle中两个日期之间每小时的记录数

时间:2012-05-23 06:36:43

标签: sql oracle group-by grouping

我需要一个在oracle中执行此序列的SINGLE查询。

select count(*) from table1
where request_time < timestamp'2012-05-19 12:00:00' and (end_time > timestamp'2012-05-19 12:00:00' or end_time=null);

select count(*) from table1
where request_time < timestamp'2012-05-19 13:00:00' and (end_time > timestamp'2012-05-19 13:00:00' or end_time=null);

select count(*) from table1
where request_time < timestamp'2012-05-19 14:00:00' and (end_time > timestamp'2012-05-19 14:00:00' or end_time=null);

select count(*) table1
where request_time < timestamp'2012-05-19 15:00:00' and (end_time > timestamp'2012-05-19 15:00:00' or end_time=null);

select count(*) from table1
where request_time < timestamp'2012-05-19 16:00:00' and (end_time > timestamp'2012-05-19 16:00:00' or end_time=null);

如你所见,时间逐渐增加。 这是输出

COUNT(*)               
1085                   

COUNT(*)               
1233                   

COUNT(*)               
1407                   

COUNT(*)               
1322                   

COUNT(*)               
1237

我写了一个查询,但它没有给我正确答案!

select col1, count(*) from
(select TO_CHAR(request_time, 'YYYY-MM-DD HH24') as col1 from table1
 where request_time <= timestamp'2012-05-19 12:00:00' and (end_time >= timestamp'2012-05-19 12:00:00' or end_time=null))
group by col1 order by col1;

这个查询给了我一个结果集,它的计数总和(*)等于上面写的第一个查询! 结果如下:

COL1          COUNT(*)               
------------- ---------------------- 
2012-05-19 07      22                     
2012-05-19 08      141                    
2012-05-19 09      322                    
2012-05-19 10      318                    
2012-05-19 11      282  

4 个答案:

答案 0 :(得分:18)

请注意trunc表达式与日期值的用法。如果未在sql * plus中运行查询,则可以省略alter session

SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

Session altered.

SQL> SELECT 
       trunc(created,'HH'), 
       count(*) 
     FROM 
       test_table 
     WHERE 
       created > trunc(SYSDATE -2) 
     group by trunc(created,'HH');


TRUNC(CREATED,'HH')   COUNT(*)
------------------- ----------
2012-05-21 09:00:00        748
2012-05-21 16:00:00         24
2012-05-21 17:00:00         12
2012-05-21 22:00:00        737
2012-05-21 23:00:00        182
2012-05-22 20:00:00         16
2012-05-22 21:00:00        293
2012-05-22 22:00:00        610

8 ROWS selected.

答案 1 :(得分:1)

您的个人查询似乎与重叠的记录集相匹配。如果您在问题中包含一些示例数据会有所帮助,但我可以猜测......

例如,所有具有end_time = null和request_time = 2012-05-19 13:30:00的记录将由第一和第二查询计数;但它们只会在“整体”查询中计算一次。

也许您打算在request_time上查询日期范围,而不是像request_time < timestamp'2012-05-19 12:00:00'这样的开放式谓词?

答案 2 :(得分:1)

对于Oracle数据库,它按预期工作。

选择        to_char(已更新,&#39; DD-MM-YYYY HH&#39;),        计数(*)      从        顾客      哪里         trunc(已更新)&gt; = to_Char(&#39; 02-JUL-2017&#39;)     并截断(更新)&lt; = to_Char(&#39; 02-JUL-2017&#39;)      按to_char分组(已更新,&#39; DD-MM-YYYY HH&#39;)

答案 3 :(得分:0)

试试这个

select TO_CHAR(request_time, 'HH24') as "hourOfDay",count(*)as
"numOfLogin", TO_CHAR(request_time, 'DD') as "date" from table1 
where request_time<= timestamp'2017-08-04 23:59:59' and
(request_time>= timestamp'2017-08-03 00:00:01' ) group by
TO_CHAR(request_time, 'HH24'),TO_CHAR(request_time, 'DD');