sql聚合大小写查询

时间:2012-08-15 21:01:51

标签: sql aggregate

我有一行查询,可以在过去24小时内每小时单独获取红色,黄色,绿色和青色项目的总和。查询返回正确的结果。但是有可能简化它吗?提前谢谢。

Set statistics io on;
DECLARE @currentTimeInMs bigint = 398244649728; -- input parameter
DECLARE @oneHourInMs bigint = 3600000; -- 1 hour = 60 mins = 3600 secs = 3600000 msecs
DECLARE @h24 bigint = @currentTimeInMs

DECLARE @h00 bigint = @h24 - (@oneHourInMs * 24)
DECLARE @h01 bigint = @h24 - (@oneHourInMs * 23)
DECLARE @h02 bigint = @h24 - (@oneHourInMs * 22)
DECLARE @h03 bigint = @h24 - (@oneHourInMs * 21)
DECLARE @h04 bigint = @h24 - (@oneHourInMs * 20)
DECLARE @h05 bigint = @h24 - (@oneHourInMs * 19)
DECLARE @h06 bigint = @h24 - (@oneHourInMs * 18)
DECLARE @h07 bigint = @h24 - (@oneHourInMs * 17)
DECLARE @h08 bigint = @h24 - (@oneHourInMs * 16)
DECLARE @h09 bigint = @h24 - (@oneHourInMs * 15)
DECLARE @h10 bigint = @h24 - (@oneHourInMs * 14)
DECLARE @h11 bigint = @h24 - (@oneHourInMs * 13)
DECLARE @h12 bigint = @h24 - (@oneHourInMs * 12)
DECLARE @h13 bigint = @h24 - (@oneHourInMs * 11)
DECLARE @h14 bigint = @h24 - (@oneHourInMs * 10)
DECLARE @h15 bigint = @h24 - (@oneHourInMs * 9)
DECLARE @h16 bigint = @h24 - (@oneHourInMs * 8)
DECLARE @h17 bigint = @h24 - (@oneHourInMs * 7)
DECLARE @h18 bigint = @h24 - (@oneHourInMs * 6)
DECLARE @h19 bigint = @h24 - (@oneHourInMs * 5)
DECLARE @h20 bigint = @h24 - (@oneHourInMs * 4)
DECLARE @h21 bigint = @h24 - (@oneHourInMs * 3)
DECLARE @h22 bigint = @h24 - (@oneHourInMs * 2)
DECLARE @h23 bigint = @h24 - (@oneHourInMs * 1)


SELECT @currentTimeInMs AS CurrentTimeInMs,
-- H00
    SUM(CASE 
        WHEN @h00 <= AnnounceStartTime AND AnnounceStartTime < @h01 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H00Red',
    SUM(CASE 
        WHEN @h00 <= AnnounceStartTime AND AnnounceStartTime < @h01 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H00Yellow',
    SUM(CASE 
        WHEN @h00 <= AnnounceStartTime AND AnnounceStartTime < @h01 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H00Green',
    SUM(CASE 
        WHEN @h00 <= AnnounceStartTime AND AnnounceStartTime < @h01 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H00Cyan',
-- H01
    SUM(CASE 
        WHEN @h01 <= AnnounceStartTime AND AnnounceStartTime < @h02 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H01Red',
    SUM(CASE 
        WHEN @h01 <= AnnounceStartTime AND AnnounceStartTime < @h02 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H01Yellow',
    SUM(CASE 
        WHEN @h01 <= AnnounceStartTime AND AnnounceStartTime < @h02 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H01Green',
    SUM(CASE 
        WHEN @h01 <= AnnounceStartTime AND AnnounceStartTime < @h02 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H01Cyan',
-- H02
    SUM(CASE 
        WHEN @h02 <= AnnounceStartTime AND AnnounceStartTime < @h03 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H02Red',
    SUM(CASE 
        WHEN @h02 <= AnnounceStartTime AND AnnounceStartTime < @h03 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H02Yellow',
    SUM(CASE 
        WHEN @h02 <= AnnounceStartTime AND AnnounceStartTime < @h03 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H02Green',
    SUM(CASE 
        WHEN @h02 <= AnnounceStartTime AND AnnounceStartTime < @h03 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H02Cyan',
-- H03
    SUM(CASE 
        WHEN @h03 <= AnnounceStartTime AND AnnounceStartTime < @h04 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H03Red',
    SUM(CASE 
        WHEN @h03 <= AnnounceStartTime AND AnnounceStartTime < @h04 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H03Yellow',
    SUM(CASE 
        WHEN @h03 <= AnnounceStartTime AND AnnounceStartTime < @h04 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H03Green',
    SUM(CASE 
        WHEN @h03 <= AnnounceStartTime AND AnnounceStartTime < @h04 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H03Cyan',
-- H04
    SUM(CASE 
        WHEN @h04 <= AnnounceStartTime AND AnnounceStartTime < @h05 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H04Red',
    SUM(CASE 
        WHEN @h04 <= AnnounceStartTime AND AnnounceStartTime < @h05 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H04Yellow',
    SUM(CASE 
        WHEN @h04 <= AnnounceStartTime AND AnnounceStartTime < @h05 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H04Green',
    SUM(CASE 
        WHEN @h04 <= AnnounceStartTime AND AnnounceStartTime < @h05 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H04Cyan',
-- H05
    SUM(CASE 
        WHEN @h05 <= AnnounceStartTime AND AnnounceStartTime < @h06 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H05Red',
    SUM(CASE 
        WHEN @h05 <= AnnounceStartTime AND AnnounceStartTime < @h06 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H05Yellow',
    SUM(CASE 
        WHEN @h05 <= AnnounceStartTime AND AnnounceStartTime < @h06 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H05Green',
    SUM(CASE 
        WHEN @h05 <= AnnounceStartTime AND AnnounceStartTime < @h06 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H05Cyan',
-- H06
    SUM(CASE 
        WHEN @h06 <= AnnounceStartTime AND AnnounceStartTime < @h07 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H06Red',
    SUM(CASE 
        WHEN @h06 <= AnnounceStartTime AND AnnounceStartTime < @h07 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H06Yellow',
    SUM(CASE 
        WHEN @h06 <= AnnounceStartTime AND AnnounceStartTime < @h07 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H06Green',
    SUM(CASE 
        WHEN @h06 <= AnnounceStartTime AND AnnounceStartTime < @h07 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H06Cyan',
-- H07
    SUM(CASE 
        WHEN @h07 <= AnnounceStartTime AND AnnounceStartTime < @h08 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H07Red',
    SUM(CASE 
        WHEN @h07 <= AnnounceStartTime AND AnnounceStartTime < @h08 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H07Yellow',
    SUM(CASE 
        WHEN @h07 <= AnnounceStartTime AND AnnounceStartTime < @h08 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H07Green',
    SUM(CASE 
        WHEN @h07 <= AnnounceStartTime AND AnnounceStartTime < @h08 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H07Cyan',
-- H08
    SUM(CASE 
        WHEN @h08 <= AnnounceStartTime AND AnnounceStartTime < @h09 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H08Red',
    SUM(CASE 
        WHEN @h08 <= AnnounceStartTime AND AnnounceStartTime < @h09 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H08Yellow',
    SUM(CASE 
        WHEN @h08 <= AnnounceStartTime AND AnnounceStartTime < @h09 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H08Green',
    SUM(CASE 
        WHEN @h08 <= AnnounceStartTime AND AnnounceStartTime < @h09 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H08Cyan',
-- H09
    SUM(CASE 
        WHEN @h09 <= AnnounceStartTime AND AnnounceStartTime < @h10 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H09Red',
    SUM(CASE 
        WHEN @h09 <= AnnounceStartTime AND AnnounceStartTime < @h10 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H09Yellow',
    SUM(CASE 
        WHEN @h09 <= AnnounceStartTime AND AnnounceStartTime < @h10 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H09Green',
    SUM(CASE 
        WHEN @h09 <= AnnounceStartTime AND AnnounceStartTime < @h10 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H09Cyan',
-- H10
    SUM(CASE 
        WHEN @h10 <= AnnounceStartTime AND AnnounceStartTime < @h11 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H10Red',
    SUM(CASE 
        WHEN @h10 <= AnnounceStartTime AND AnnounceStartTime < @h11 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H10Yellow',
    SUM(CASE 
        WHEN @h10 <= AnnounceStartTime AND AnnounceStartTime < @h11 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H10Green',
    SUM(CASE 
        WHEN @h10 <= AnnounceStartTime AND AnnounceStartTime < @h11 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H10Cyan',
-- H11
    SUM(CASE 
        WHEN @h11 <= AnnounceStartTime AND AnnounceStartTime < @h12 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H11Red',
    SUM(CASE 
        WHEN @h11 <= AnnounceStartTime AND AnnounceStartTime < @h12 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H11Yellow',
    SUM(CASE 
        WHEN @h11 <= AnnounceStartTime AND AnnounceStartTime < @h12 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H11Green',
    SUM(CASE 
        WHEN @h11 <= AnnounceStartTime AND AnnounceStartTime < @h12 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H11Cyan',
-- H12
    SUM(CASE 
        WHEN @h12 <= AnnounceStartTime AND AnnounceStartTime < @h13 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H12Red',
    SUM(CASE 
        WHEN @h12 <= AnnounceStartTime AND AnnounceStartTime < @h13 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H12Yellow',
    SUM(CASE 
        WHEN @h12 <= AnnounceStartTime AND AnnounceStartTime < @h13 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H12Green',
    SUM(CASE 
        WHEN @h12 <= AnnounceStartTime AND AnnounceStartTime < @h13 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H12Cyan',
-- H13
    SUM(CASE 
        WHEN @h13 <= AnnounceStartTime AND AnnounceStartTime < @h14 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H13Red',
    SUM(CASE 
        WHEN @h13 <= AnnounceStartTime AND AnnounceStartTime < @h14 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H13Yellow',
    SUM(CASE 
        WHEN @h13 <= AnnounceStartTime AND AnnounceStartTime < @h14 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H13Green',
    SUM(CASE 
        WHEN @h13 <= AnnounceStartTime AND AnnounceStartTime < @h14 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H13Cyan',
-- H14
    SUM(CASE 
        WHEN @h14 <= AnnounceStartTime AND AnnounceStartTime < @h15 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H14Red',
    SUM(CASE 
        WHEN @h14 <= AnnounceStartTime AND AnnounceStartTime < @h15 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H14Yellow',
    SUM(CASE 
        WHEN @h14 <= AnnounceStartTime AND AnnounceStartTime < @h15 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H14Green',
    SUM(CASE 
        WHEN @h14 <= AnnounceStartTime AND AnnounceStartTime < @h15 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H14Cyan',
-- H15
    SUM(CASE 
        WHEN @h15 <= AnnounceStartTime AND AnnounceStartTime < @h16 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H15Red',
    SUM(CASE 
        WHEN @h15 <= AnnounceStartTime AND AnnounceStartTime < @h16 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H15Yellow',
    SUM(CASE 
        WHEN @h15 <= AnnounceStartTime AND AnnounceStartTime < @h16 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H15Green',
    SUM(CASE 
        WHEN @h15 <= AnnounceStartTime AND AnnounceStartTime < @h16 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H15Cyan',
-- H16
    SUM(CASE 
        WHEN @h16 <= AnnounceStartTime AND AnnounceStartTime < @h17 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H16Red',
    SUM(CASE 
        WHEN @h16 <= AnnounceStartTime AND AnnounceStartTime < @h17 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H16Yellow',
    SUM(CASE 
        WHEN @h16 <= AnnounceStartTime AND AnnounceStartTime < @h17 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H16Green',
    SUM(CASE 
        WHEN @h16 <= AnnounceStartTime AND AnnounceStartTime < @h17 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H16Cyan',
-- H17
    SUM(CASE 
        WHEN @h17 <= AnnounceStartTime AND AnnounceStartTime < @h18 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H17Red',
    SUM(CASE 
        WHEN @h17 <= AnnounceStartTime AND AnnounceStartTime < @h18 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H17Yellow',
    SUM(CASE 
        WHEN @h17 <= AnnounceStartTime AND AnnounceStartTime < @h18 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H17Green',
    SUM(CASE 
        WHEN @h17 <= AnnounceStartTime AND AnnounceStartTime < @h18 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H17Cyan',
-- H18
    SUM(CASE 
        WHEN @h18 <= AnnounceStartTime AND AnnounceStartTime < @h19 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H18Red',
    SUM(CASE 
        WHEN @h18 <= AnnounceStartTime AND AnnounceStartTime < @h19 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H18Yellow',
    SUM(CASE 
        WHEN @h18 <= AnnounceStartTime AND AnnounceStartTime < @h19 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H18Green',
    SUM(CASE 
        WHEN @h18 <= AnnounceStartTime AND AnnounceStartTime < @h19 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H18Cyan',
-- H19
    SUM(CASE 
        WHEN @h19 <= AnnounceStartTime AND AnnounceStartTime < @h20 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H19Red',
    SUM(CASE 
        WHEN @h19 <= AnnounceStartTime AND AnnounceStartTime < @h20 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H19Yellow',
    SUM(CASE 
        WHEN @h19 <= AnnounceStartTime AND AnnounceStartTime < @h20 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H19Green',
    SUM(CASE 
        WHEN @h19 <= AnnounceStartTime AND AnnounceStartTime < @h20 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H19Cyan',
-- H20
    SUM(CASE 
        WHEN @h20 <= AnnounceStartTime AND AnnounceStartTime < @h21 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H20Red',
    SUM(CASE 
        WHEN @h20 <= AnnounceStartTime AND AnnounceStartTime < @h21 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H20Yellow',
    SUM(CASE 
        WHEN @h20 <= AnnounceStartTime AND AnnounceStartTime < @h21 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H20Green',
    SUM(CASE 
        WHEN @h20 <= AnnounceStartTime AND AnnounceStartTime < @h21 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H20Cyan',
-- H21
    SUM(CASE 
        WHEN @h21 <= AnnounceStartTime AND AnnounceStartTime < @h22 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H21Red',
    SUM(CASE 
        WHEN @h21 <= AnnounceStartTime AND AnnounceStartTime < @h22 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H21Yellow',
    SUM(CASE 
        WHEN @h21 <= AnnounceStartTime AND AnnounceStartTime < @h22 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H21Green',
    SUM(CASE 
        WHEN @h21 <= AnnounceStartTime AND AnnounceStartTime < @h22 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H21Cyan',
-- H22
    SUM(CASE 
        WHEN @h22 <= AnnounceStartTime AND AnnounceStartTime < @h23 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H22Red',
    SUM(CASE 
        WHEN @h22 <= AnnounceStartTime AND AnnounceStartTime < @h23 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H22Yellow',
    SUM(CASE 
        WHEN @h22 <= AnnounceStartTime AND AnnounceStartTime < @h23 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H22Green',
    SUM(CASE 
        WHEN @h22 <= AnnounceStartTime AND AnnounceStartTime < @h23 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H22Cyan',
-- H23
    SUM(CASE 
        WHEN @h23 <= AnnounceStartTime AND AnnounceStartTime < @h24 AND Kind=0 AND (Severity=0x400)
        THEN 1 ELSE 0
        END) AS 'H23Red',
    SUM(CASE 
        WHEN @h23 <= AnnounceStartTime AND AnnounceStartTime < @h24 AND Kind=0 AND (Severity=0x200)
        THEN 1 ELSE 0
        END) AS 'H23Yellow',
    SUM(CASE 
        WHEN @h23 <= AnnounceStartTime AND AnnounceStartTime < @h24 AND Kind=0 AND (Severity=0x100)
        THEN 1 ELSE 0
        END) AS 'H23Green',
    SUM(CASE 
        WHEN @h23 <= AnnounceStartTime AND AnnounceStartTime < @h24 AND Kind=1
        THEN 1 ELSE 0
        END) AS 'H23Cyan'   

FROM Alert A
WHERE A.AnnounceStartTime BETWEEN @h00 AND @h24

2 个答案:

答案 0 :(得分:0)

这是一种可以简化它的方法。您可以为每个元素创建标记,然后将它们相乘:

select sum(isHH00*isRed) as HH00Red,
       sum(isHH00*isYellow) as HH00Yellow,
       . . .
from (select a.*,
             (case when Kind=0 AND (Severity=0x400) then 1 else 0 end) as IsRed,
             . . .
             (case when @h00 <= AnnounceStartTime AND AnnounceStartTime < @h01
                   then 1 else 0
              end) as isHH00,
             . . .
      from Alert a
     ) a
where . . .

您的另一种选择是通过执行以下操作对查询进行分组:

select datepart(hh, AnnounceStartTime), kind, Severity, count(*) as cnt
from Alert a
group by datepart(hh, AnnounceStartTime), kind, Severity

然后使用PIVOT语句或其他工具(如Excel)将其转换为您需要的数据。

答案 1 :(得分:0)

我首先要创建一个临时汇总表:

declare
  @one_hour_in_ms    int      ,
  @dtNow             datetime ,
  @@dtNowLess24Hours datetime

set @one_hour_in_ms   = 60 * 60 * 1000
 set @dtNow            = current_timestamp        -- current date and time
 set @dtNowLess24Hours = dateadd(hour,-24,@dtNow)

create table #work
(
  period_id int not null primary key clustered ,
  red       int not null ,
  yellow    int not null ,
  green     int not null ,
  cyan      int not null ,
)

insert #work ( period_id , red , yellow , green , cyan )
select period_id = datediff(ms,@startofPeriod,a.AnnounceStartTime) / @one_hour_in_ms , -- creates period id with the domain 0-23 indicating the hour offset within the reporting period
       red       = sum( case when a.Kind = 0 and a.Severity = 0x400 then 1 else 0 end ) ,
       yellow    = sum( case when a.Kind = 0 and a.Severity = 0x200 then 1 else 0 end ) ,
       green     = sum( case when a.Kind = 0 and a.Severity = 0x100 then 1 else 0 end ) ,
       cyan      = sum( case when a.Kind = 1                        then 1 else 0 end )
from Alert a
where a.AnnounceStartTime >= @startOfPeriod
  and a.AnnounceStartTime <  @dtNow
group by datediff(ms,@startofPeriod,a.AnnounceStartTime) / @one_hour_in_ms

上面创建了一个临时表,最多24行,在24小时报告期内每小时一行。当然,如果没有记录该期间的数据,则不会生成任何行。

如果这就足够了,只需将其作为多行结果集返回,然后让客户端将行旋转为列。否则,您需要执行表旋转。一种不依赖于pivot的实现不可知的方法就是这样做:

select dtFrom  = @dtNowLess24Hours ,
       dtThru  = @dtNow            ,
       H00_Red = sum( p00.red ) , H00_Yellow = sum( p00.yellow ) , H00_Green = sum( p00.green ) , H00_Cyan = sum( p00.cyan ) ,
       H01_Red = sum( p01.red ) , H01_Yellow = sum( p01.yellow ) , H01_Green = sum( p01.green ) , H01_Cyan = sum( p01.cyan ) ,
       ...
       H23_Red = sum( p23.red ) , H23_Yellow = sum( p23.yellow ) , H23_Green = sum( p23.green ) , H23_Cyan = sum( p23.cyan )
from       ( select * from #work where period_id = 0  ) p00
cross join ( select * from #work where period_id = 1  ) p01
...
cross join ( select * from #work where period_id = 23 ) p23

如果您的系统支持pivot,则整个过程要简单得多。