2011-01-25 61 views
4

好吧,試圖構建單個查詢來爲自己節省一大堆時間(而不是寫大量的單獨查詢),但是我不'甚至不知道該如何開始。SQL/Oracle按給定日期的某個類型按字段分組數據

我需要的是看看single day and type,並在上午8點至晚上8點之間按小時計算行動。因此,例如我有以下的假表

TYPE_ ACTION_  TIMESTAMP_ 
------------------------------ 
A  processed 2010-11-19 10:00:00.000 
A  processed 2010-11-19 10:46:45.000 
A  processed 2010-11-19 11:46:45.000 
A  processed 2010-11-19 12:46:45.000 
A  processed 2010-11-19 12:48:45.000 
A  pending  2010-11-19 11:46:45.000 
A  pending  2010-11-19 11:50:45.000 
A  pending  2010-11-19 12:46:45.000 
A  pending  2010-11-19 12:48:45.000 
B  pending  2010-11-19 19:48:45.000 
B  pending  2010-11-19 21:46:45.000 
.etc 

所以,如果我想看看所有的記錄與

  • TYPE_ = 'A'
  • 上日期2010-11-19
  • 通過ACTION_每小時

分組我會看到這個結果

ACTION_ NUMOCCURENCES RANGE 
--------------------------------------------- 
processed 2    10:00:00 - 11:00:00 
pending 0    10:00:00 - 11:00:00 
processed 1    11:00:00 - 12:00:00 
pending 2    11:00:00 - 12:00:00 
processed 2    12:00:00 - 13:00:00 
pending 2    12:00:00 - 13:00:00 

或類似的東西,但至少應該提供一個我正在尋找的東西的想法。

任何人都可以幫忙嗎?通常我會嘗試提供一些我正在使用的示例代碼,但我不知道如何通過條件來實現這一點。

+0

你能提供DDL和INSERTs嗎? – Chandu 2011-01-25 21:28:57

回答

7
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'); 
+0

過濾像`和8 * 19之間的1 * to_char(TIMESTAMP_,'hh24')? – RichardTheKiwi 2011-01-25 21:38:58

+0

哇,快!不幸的是,這是Oracle向我反饋的「不是GROUP BY表達式」 – dscl 2011-01-25 21:41:17

3
select 
    ACTION_ 
    count(*) NUMOCCURENCES, 
    to_char(TIMESTAMP_, 'hh24') || ':00:00 - ' || to_char(TIMESTAMP_ + 1/24, 'hh24') || ':00:00' RANGE 
from tbl 
where TIMESTAMP_ between DATE '2010-11-19' and DATE '2010-11-20' 
    and TYPE_ = 'A' 
    and 1 * to_char(TIMESTAMP_, 'hh24') between 8 and 19 
group by ACTION_, to_char(TIMESTAMP_, 'hh24'), to_char(TIMESTAMP_ + 1/24, 'hh24') 
order by RANGE, ACTION_ desc