Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在oracle中按小时计算两个日期之间的记录数_Sql_Oracle_Group By_Grouping - Fatal编程技术网

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

Sql 在oracle中按小时计算两个日期之间的记录数,sql,oracle,group-by,grouping,Sql,Oracle,Group By,Grouping,我需要在oracle中执行此序列的单个查询 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

我需要在oracle中执行此序列的单个查询

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);





我写了一个问题,但它没有给我正确的答案

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;

您的单个查询似乎与重叠的记录集相匹配。如果你在你的问题中加入一些样本数据,这会有所帮助,但我可以猜


例如,第一次查询和第二次查询都将统计所有结束时间为null、请求时间为2012-05-19 13:30:00的记录;但在“总体”查询中,它们只计算一次


也许您想查询请求时间的日期范围,而不是像
request\u time

注意
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.

对于Oracle数据库,其工作正常

挑选 至_char(已更新,'DD-MM-YYYY HH'), 计数(*) 从…起 顾客 哪里 trunc(更新)>=至_Char('2017年7月2日' 和trunc(更新)试试这个

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');
选择将字符(请求时间,'HH24')作为“hourOfDay”,将(*)作为
“numOfLogin”,将表1中的字符(请求时间,'DD')作为“日期”
其中请求时间=时间戳'2017-08-03 00:00:01')分组人
TO_CHAR(请求时间,'HH24')、TO_CHAR(请求时间,'DD');

“将由第一个和第二个查询计算”是的,我需要这个。像斐波那契!这个小时的计数是前几个小时的计数+这个小时的计数。我有两个日期,我想在两个日期之间计算这个查询。然后我想扩展这个查询,以按天、月甚至年获得结果!谢谢,但这给了我同样的结果,我写的小组。问题是:我需要22:00:00小时的总和,在你的例子中是748+24+12+737+182+16+293+610=2622,而16:00:00小时应该是748+24=772,依此类推……我想我不明白你的意思。@Heidarzadeh:如果你说的是运行
COUNT(*)的总和
,请用
COUNT(*)试试ipip的解决方案
替换为
sum(count(*)over(order by trunc(created,'HH'))
@Andriy M我尝试了您的解决方案,但得到了这个错误“没有一个单独的组函数”@Heidarzadeh:您确定
trunc(created,'HH')
仍然包含在
group by
中吗?
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  
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.
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');