2014-10-28 32 views
1

我希望如此,我在一列所有的结果,而不是单独的4对到那些下面的SQL语句组合:结合SQL语句和包括时间字符串范围

select count(incnum) as counttenth3 from kincident where (to_char(reportdate,'hh24') between 0 and 2.59 or to_char(reportdate,'hh24') between 21 and 23.59 

select count(incnum) as counttenth2 from kincident where to_char(reportdate,'hh24') between 15 and 20.59 

select count(incnum) as counttenth1 from kincident where to_char(reportdate,'hh24') between 9 and 14.59 

select count(incnum) as counttenth0 from kincident where to_char(reportdate,'hh24') between 3 and 8.59 

唯一的区别是每个陈述的时间范围。 因此,我试图将它们合并为一列,并且我希望第二列包含给定字符串的行(不是来自数据库)

例如,

Timing | count of incidents 
----------------------------- 
morning | 26 
afternoon | 35 
night  | 40 
+0

所以,你要想想这2个步骤 - 类别到您的时间 – 2014-10-28 06:57:21

+0

的类别,然后将范围组@ChrisMoutray这是我遇到的麻烦是什么,我不知道如何为某个时间范围分配一个类别。我所拥有的就是个人4条语句,每条语句都是[早,午,晚,午夜]之一] – AuthenticReplica 2014-10-28 07:01:49

回答

0

事情是这样的 - 未经检验的,因为我一般写MS SQL :)

select 
    t.time_of_day, 
    count(t.time_of_day) 
from 
(
    --- 
    select 
     case to_char(reportdate,'hh24') 
      when between 15 and 20.59 then 'afternoon' 
      when between 9 and 14.59 then 'morning' 
      when between 3 and 8.59 then 'early-morning' 
      else 'night' --when between 0 and 2.59 or to_char(reportdate,'hh24') between 21 and 23.59 
     end as time_of_day  
    from 
     kincident 
) t 
group by 
    t.time_of_day 

使用case语句类别你的时间范围,我已经把它放在一个子查询中,但是你可以在视图中换行;作为外部查询的一部分,我们将时间分类分组,然后在外部选择中计数。

+0

似乎在case语句中有错误的语法。 – 2014-10-28 07:33:06

1

看来想要组通过一个case语句:

select 
    case when to_char(reportdate,'hh24') between 3 and 8.59 then 'morning' 
     when to_char(reportdate,'hh24') between 9 and 14.59 then 'noon' 
     when to_char(reportdate,'hh24') between 15 and 20.59 then 'afternoon' 
     else 'night' end as range 
    ,count(incnum) 
from 
    kincident 
group by 
    case when to_char(reportdate,'hh24') between 3 and 8.59 then 'morning' 
     when to_char(reportdate,'hh24') between 9 and 14.59 then 'noon' 
     when to_char(reportdate,'hh24') between 15 and 20.59 then 'afternoon' 
     else 'night' end 
0

作为Jaugar Chang的回答: 的替代方法1.)要获取常量值,只需使用字符串常量而不是cloumn名称。 2.)您可以使用union将查询合并为一个。

所以它会是这样的:

select 'night', count(incnum) as incidentcout from kincident where (to_char(reportdate,'hh24') between 0 and 2.59 or to_char(reportdate,'hh24') between 21 and 23.59 
union 
select 'afternoon', count(incnum) as incidentcout from kincident where to_char(reportdate,'hh24') between 15 and 20.59 
union 
select 'noon', count(incnum) as incidentcout as counttenth1 from kincident where to_char(reportdate,'hh24') between 9 and 14.59 
union 
select 'morning', count(incnum) as incidentcout from kincident where to_char(reportdate,'hh24') between 3 and 8.59 
1

我重做这个使用查找表的时间为加入反对。添加一个有24行的TIME_CATEGORY表,将每个小时映射到“时间类别”。为什么?它可以通过系统/查询的其他部分重复使用,并且还允许您从一个地方实施具有中央控制的基于时间的规则引擎。 (一旦你开始写作案例陈述的路线,你经常会到处重复自己)。

create table time_category(
    hour integer primary key, 
    category varchar2(20) 
) organization index; 

-- populate categories 
begin 
    for i in 0 .. 2 loop 
    insert into time_category values(i, 'Night'); 
    end loop; 
    for i in 3 .. 8 loop 
    insert into time_category values(i, 'Morning'); 
    end loop; 
    for i in 9 .. 14 loop 
    insert into time_category values(i, 'Noon'); 
    end loop; 
    for i in 15 .. 20 loop 
    insert into time_category values(i, 'Afternoon'); 
    end loop; 
    for i in 21 .. 23 loop 
    insert into time_category values(i, 'Night'); 
    end loop; 
end; 
/

然后将查询变为:

-- Join incident table to time categories by hour of reportdate 

select category as Timing, count(1) as "Count of incidents" 
    from kincident i join time_category tc on extract(hour from i.reportdate) = tc.hour 
    group by category 
; 
+1

我喜欢这个答案:) – 2014-10-28 07:28:52

+0

@ChrisMoutray谢谢你的队友! – codenheim 2014-10-28 07:30:28