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);
ご覧のとおり、時間は1つずつ増えています。これが出力です
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