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