我有一个基本上包含错误日志的表。这些错误基于各种因素而到来并且具有各种严重性。我试图在24小时内查询每小时每个严重性的总和。
我有它的工作,除了一个小项目。结果按小时排序,小时始终从0开始。我需要的是它在真正的时间序列中。这意味着如果查询在6月23日中午运行,则查询需要在6/22中午有第一条记录。
样本表数据: ID已创建严重性
FL41988194-51133 4/13/2014 20:21 critical
JO03982444-74849 4/14/2014 12:46 major
JO03982444-74852 4/14/2014 12:46 major
JO03982444-74855 4/14/2014 12:46 major
BY79194841-06182 4/19/2014 19:54 major
BY79194841-06183 4/19/2014 19:54 major
BY79194841-06184 4/19/2014 19:54 major
TV90425333-88384 5/20/2014 7:02 major
FZ23706935-25024 6/7/2014 14:56 major
SY05532197-47119 6/12/2014 3:57 major
这是我目前的代码:
select
[Hour], ISNULL([Critical],0) as Critical, ISNULL([Major],0) as Major, ISNULL([Minor],0) as Minor, ISNULL([Warning],0) as Warning,
ISNULL([Information],0) as Information,
ISNULL([Critical],0) + ISNULL([Warning],0) + ISNULL([Major],0) + ISNULL([Minor],0) + ISNULL([Information],0) as [Total]
from
(SELECT
DATEPART(hh, created) as 'Hour',
[severity],
count([id]) as incidents
FROM [ALARM_TRANSACTION_SUMMARY]
where created >= GETDATE()-1
group by DATEPART(hh, created), severity
**Union ALL
select NumberValue, NULL, NULL
From NumberTable**
) PS
PIVOT
(
SUM (incidents)
FOR severity IN
( [Critical], [Information], Major, Minor, Warning)
) AS pvt
Current输出如下所示:
Hour Critical Major Minor Warning Information Total
---- -------- ----- ----- ------- ----------- ----- 0 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 1 0 0 0 1 4 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0 1 0 0 0 1 8 0 0 0 0 0 0 9 0 0 0 0 0 0 10 0 0 0 0 0 0 11 0 0 0 0 0 0 12 0 3 0 0 0 3 13 0 0 0 0 0 0 14 0 1 0 0 0 1 15 0 0 0 0 0 0 16 0 0 0 0 0 0 17 0 0 0 0 0 0 18 0 0 0 0 0 0 19 0 2 0 0 0 2 20 1 0 0 0 0 1 21 0 0 0 0 0 0 22 0 0 0 0 0 0 23 0 0 0 0 0 0
正如我上面所说,这确实给了我每小时的总数,但总数是错误的排序顺序...所以如果我在下午2点运行它,我需要输出看起来像这样:
Hour Critical Major Minor Warning Information Total
---- -------- ----- ----- ------- ----------- ----- 14 0 1 0 0 0 1 15 0 0 0 0 0 0 16 0 0 0 0 0 0 17 0 0 0 0 0 0 18 0 0 0 0 0 0 19 0 2 0 0 0 2 20 1 0 0 0 0 1 21 0 0 0 0 0 0 22 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 1 0 0 0 1 4 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0 1 0 0 0 1 8 0 0 0 0 0 0 9 0 0 0 0 0 0 10 0 0 0 0 0 0 11 0 0 0 0 0 0 12 0 3 0 0 0 3 13 0 0 0 0 0 0
另一件事。这正在提供一份报告,要求数据如上所示(图表软件不够智能,无法使用原始数据和时间戳的小计)。
答案 0 :(得分:1)
(original query)
ORDER BY
CASE WHEN [Hour] >= DATEPART(HOUR, GETDATE())
THEN [Hour]
ELSE [Hour] + 24
END
答案 1 :(得分:1)
尝试此查询。
select
[Hour], ISNULL([Critical],0) as Critical, ISNULL([Major],0) as Major, ISNULL([Minor],0) as Minor, ISNULL([Warning],0) as Warning,
ISNULL([Information],0) as Information,
ISNULL([Critical],0) + ISNULL([Warning],0) + ISNULL([Major],0) + ISNULL([Minor],0) + ISNULL([Information],0) as [Total]
from
(SELECT
DATEPART(hh, created) as 'Hour',
DATEPART(hh, DATEADD(hh,-DATEPART(hh, created),GetDate())) AS SortHour,
[severity],
count([id]) as incidents
FROM [ALARM_TRANSACTION_SUMMARY]
where created >= GETDATE()-1
group by DATEPART(hh, created), severity
Union ALL
select NumberValue,DATEPART(hh, DATEADD(hh,-NumberValue,GetDate())), NULL, NULL
From NumberTable
) PS
PIVOT
(
SUM (incidents)
FOR severity IN
( [Critical], [Information], Major, Minor, Warning)
) AS pvt
order by SortHour
答案 2 :(得分:0)
UNTESTED但从外表看这应该有效;我认为我们不需要将它包含在选择中。
SELECT
[Hour], ISNULL([Critical],0) as Critical, ISNULL([Major],0) as Major, ISNULL([Minor],0) as Minor, ISNULL([Warning],0) as Warning,
ISNULL([Information],0) as Information,
ISNULL([Critical],0) + ISNULL([Warning],0) + ISNULL([Major],0) + ISNULL([Minor],0) + ISNULL([Information],0) as [Total]
FROM
(SELECT
DATEPART(hh, created) as 'Hour',
[severity],
count([id]) as incidents,
max(created) as SortDate
FROM [ALARM_TRANSACTION_SUMMARY]
where created >= GETDATE()-1
group by DATEPART(hh, created), severity, max(created)
) PS
PIVOT
(
SUM (incidents)
FOR severity IN
( [Critical], [Information], Major, Minor, Warning)
) AS pvt
ORDER BY sortDate desc;