我有这个LINK,
为什么直到最后一行才算?
我想要输出直到最后一行。
它计算STATUS = 0的间隔
CREATE TABLE [Alerts]
(
[Timestamp] datetime,
[Status] int
)
INSERT INTO [Alerts] ([Timestamp], [Status])
VALUES
('2013-1-1 00:00:00', 1),
('2013-1-1 00:00:05', 1),
('2013-1-1 00:00:10', 2),
('2013-1-1 00:00:15', 2),
('2013-1-1 00:00:20', 0),
('2013-1-1 00:00:25', 1),
('2013-1-1 00:00:30', 1),
('2013-1-1 00:00:32', 2),
('2013-1-1 00:00:35', 2),
('2013-1-1 00:00:40', 0),
('2013-1-1 00:00:45', 0),
('2013-1-1 00:00:50', 0)
我试过这个:
SELECT
MIN ([main].[Start]) AS [STOP_Begin],
[main].[End] AS [STOP_End],
DATEDIFF(s, MIN([main].[Start]), [main].[End]) AS [Interval_Second]
FROM
(
SELECT
[sub].[Start],
MIN([sub].[End]) AS [End]
FROM
(
SELECT
[start].[Timestamp] AS [Start],
[start].[Status] AS [StartingStatus],
[end].[Timestamp] AS [End],
[end].[Status] AS [EndingStatus]
FROM [Alerts] [start], [Alerts] [end]
WHERE [start].[Status] = 0
AND [start].[Timestamp] < [end].[Timestamp]
AND [start].[Status] <> [end].[Status]
) AS [sub]
GROUP BY
[sub].[Start],
[sub].[StartingStatus]
) AS [main]
GROUP BY
[main].[End]
它给出输出:
我希望输出直到最后一行,如果0是最后一行,它给我Interval=NULL,我的问题
它直到最后一行0才算在内
我想要的结果:
---------STOP_BEGIN----- --------STOP_END--------- ----INTERVAL_SECOND-----<br />
January, 01 2013 00:00:20+0000 January, 01 2013 00:00:25+0000 5
January, 01 2013 00:00:40+0000 NULL NULL