0

我有一行查询来获取过去 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
4

2 回答 2

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 等其他工具将其转换为您需要的数据。

于 2012-08-15T21:53:25.587 回答
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,那么整个过程就简单多了。

于 2012-08-15T22:57:33.867 回答