0

過去24時間の1時間ごとに、赤、黄、緑、シアンのアイテムの合計を個別に取得する1行のクエリがあります。クエリは正しい結果を返します。しかし、それを単純化することは可能ですか?前もって感謝します。

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

これを単純化できる 1 つの方法を次に示します。各要素のフラグを作成してから、それらを掛け合わせることができます。

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 時間のレポート期間の各時間に 1 つずつ、最大 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 に答える