SQL/Oracle按特定日期的某种类型按字段分组数据

dsc*_*scl 4 sql oracle grouping timestamp

好吧,尝试构建一个单独的查询来节省我自己一大堆时间(而不是写大量的单独查询),但我甚至不知道如何开始这个.

我需要的是single day and type在上午8:00到晚上8:00之间按小时查看和分析行动计数.所以例如我有以下假表

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
Run Code Online (Sandbox Code Playgroud)

所以,如果我想查看所有记录

  • TYPE_ ='A'
  • 日期2010-11-19
  • 按行动每小时分组

我会看到这个结果

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
Run Code Online (Sandbox Code Playgroud)

或类似的东西,但至少应该知道我在寻找什么.

有人可以帮忙吗?通常情况下,我会尝试提供一些我正在使用的示例代码,但我不知道如何通过实现此目的所需的子句来处理组.

Ren*_*ger 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;
Run Code Online (Sandbox Code Playgroud)

现在,上面的select语句只返回至少有on动作的小时数.为了显示所有小时 - {处理/待定}组合的记录,应对查询进行以下修改:

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_;
Run Code Online (Sandbox Code Playgroud)

顺便说一句,这是我使用的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');
Run Code Online (Sandbox Code Playgroud)