select
action_,
count(*) as numoccurences,
to_char(timestamp_ , 'hh24') || ':00:00-' ||
to_char(timestamp_ + 1/24, 'hh24') || ':00:00' as range
from
tq84_action
where
timestamp_ between timestamp '2010-11-19 08:00:00' and
timestamp '2010-11-19 20:00:00' and
type_ = 'A'
group by
action_,
to_char(timestamp_ , 'hh24') || ':00:00-' ||
to_char(timestamp_ + 1/24, 'hh24') || ':00:00'
order by
range;
現在,上面的select語句只返回小時其中至少有行動。爲了顯示所有小時的紀錄 - {處理/未決}組合,下列修訂提出,要查詢:
select
action_,
count(type_) as numoccurences,
to_char(timestamp_ , 'hh24') || ':00:00-' ||
to_char(timestamp_ + 1/24, 'hh24') || ':00:00' as range_
from (
select * from tq84_action
where
timestamp_ between timestamp '2010-11-19 08:00:00' and
timestamp '2010-11-19 20:00:00' and
type_ = 'A'
union all (
select
null as type_,
action.name_ as action_,
date '2010-11-19' + 8/24 + hour.counter_/24 as timestamp_1
from (
select
level-1 counter_
from dual
connect by level <= 12
) hour,
(
select 'processed' as name_ from dual union all
select 'pending' as name_ from dual
) action
)
)
group by
action_,
to_char(timestamp_ , 'hh24') || ':00:00-' ||
to_char(timestamp_ + 1/24, 'hh24') || ':00:00'
order by
range_;
順便說一句,這裏的DDL和DML我用:
drop table tq84_action;
create table tq84_action (
type_ varchar2(1),
action_ varchar2(10),
timestamp_ timestamp
);
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 10:00:00.000');
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 10:46:45.000');
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 11:46:45.000');
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 12:46:45.000');
insert into tq84_action values('A', 'processed' , timestamp '2010-11-19 12:48:45.000');
insert into tq84_action values('A', 'pending' , timestamp '2010-11-19 11:46:45.000');
insert into tq84_action values('A', 'pending' , timestamp '2010-11-19 11:50:45.000');
insert into tq84_action values('A', 'pending' , timestamp '2010-11-19 12:46:45.000');
insert into tq84_action values('A', 'pending' , timestamp '2010-11-19 12:48:45.000');
insert into tq84_action values('B', 'pending' , timestamp '2010-11-19 19:48:45.000');
insert into tq84_action values('B', 'pending' , timestamp '2010-11-19 21:46:45.000');
你能提供DDL和INSERTs嗎? – Chandu 2011-01-25 21:28:57