6

我需要一个在 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;

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

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

5 回答 5

24

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

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.
于 2012-05-23T11:07:37.750 回答
1

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

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

也许您打算查询 request_time 上的日期范围,而不是像request_time < timestamp'2012-05-19 12:00:00'?

于 2012-05-23T07:05:43.543 回答
1

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

SELECT to_char(updated,'DD-MM-YYYY HH'), count(*) FROM customer WHERE trunc(updated) >= to_Char('02-JUL-2017') And trunc(updated) <= to_Char('02- JUL-2017') 分组 to_char(更新,'DD-MM-YYYY HH')

于 2017-07-03T16:45:08.893 回答
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');
于 2017-08-04T14:04:33.153 回答
0

我必须做你想做的同样的事情。这是我想出的逻辑

select count(0), hours
from
(select to_char(from_date + ((to_number(column_value)-1)/24), 'yyyy-mm-dd hh24')||':00:00' hours
  from table1,
       xmltable ('for $i in 1 to xs:int(.) return $i'
                 passing xmlelement(e, extract (hour from cast(to_date as timestamp)-cast(from_date as timestamp))+2))
where change_date > sysdate - 1
)
group by hours
order by count(0) desc;
于 2021-12-16T01:26:51.143 回答