我需要一個在oracle中執行這個序列的SINGLE查詢。在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);
正如你所看到的小時逐一增加。 這裏是輸出
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
謝謝,但這給了我同樣的結果,我已經寫了小組。問題是:我需要這個時間22:00:00,這在你的例子中是748 + 24 + 12 + 737 + 182 + 16 + 293 + 610 = 2622,而對於小時16:00:00它應該是748 + 24 = 772等等...... – Heidarzadeh
我想我不理解你。 – ipip
@Heidarzadeh:如果你正在討論總計COUNT(*)',試着用'count(*)'替換'sum(count(*))over(用trunc命令創建'HH' ))'。 –