组合SQL语句并包含时间范围的字符串

时间:2014-10-28 06:53:31

标签: sql oracle

我想要合并以下SQL语句,以便将所有结果放在一列而不是4列中:

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

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

E.g。

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

4 个答案:

答案 0 :(得分:1)

似乎你想通过案例陈述分组:

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

答案 1 :(得分: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
;

答案 2 :(得分: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语句对您的时间范围进行分类,我将其放入子查询中,但您可以将其包装在视图中;作为外部查询的一部分,我们将时间类别分组,然后在外部选择中计数。

答案 3 :(得分: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