Counting number of records hour by hour between tw

2019-01-18 08:03发布

I need a SINGLE query that does this sequence in 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);

As you see the hour is increasing one by one. here is the output

COUNT(*)               
1085                   

COUNT(*)               
1233                   

COUNT(*)               
1407                   

COUNT(*)               
1322                   

COUNT(*)               
1237

I have written a query but it does not give me the right answer!

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;

this query gives me a result set that sum of it's count(*) is equal to the first query written above! here is the result:

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条回答
Summer. ? 凉城
2楼-- · 2019-01-18 08:24

Note the usage of trunc expression with date values. You can omit the alter session if you are not running the query in 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.
查看更多
\"骚年 ilove
3楼-- · 2019-01-18 08:30

Your individual queries seem to be matching overlapping sets of records. It would help if you included some sample data in your question, but I can guess...

For example, all the records which have an end_time=null and a request_time=2012-05-19 13:30:00 will be counted by both the first and second queries; but they will only be counted once in your "overall" query.

Maybe you meant to query on a date range on request_time, instead of having an open-ended predicate like request_time < timestamp'2012-05-19 12:00:00'?

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-18 08:30

Try this

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');
查看更多
唯我独甜
5楼-- · 2019-01-18 08:35

For Oracle database its working as expected.

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') group by to_char(updated,'DD-MM-YYYY HH')

查看更多
登录 后发表回答