我在编写查询时遇到困难。我需要将时间连续的状态行合并成一个状态。例如,给定数据:当一个的结束时间是另一个的开始时间时合并行
start end state seconds
2011-04-21 08:13:30.000 2011-04-21 08:18:00.000 STATE1 270
2011-04-21 08:18:00.000 2011-04-21 08:22:30.000 STATE1 270
2011-04-21 08:22:30.000 2011-04-21 08:26:26.000 STATE1 236
2011-04-21 08:26:26.000 2011-04-21 08:26:47.000 STATE2 21
2011-04-21 08:26:47.000 2011-04-21 08:27:30.000 STATE3 43
2011-04-21 08:27:30.000 2011-04-21 08:28:20.000 STATE1 50
2011-04-21 08:40:30.000 2011-04-21 08:41:00.000 STATE1 30
我需要结合起来,只有当row2.state = row1.state AND row2.start = row1.end
行并拿出状态的全面启动和结束时间。结果应该是:
start end state seconds
2011-04-21 08:13:30.000 2011-04-21 08:26:26.000 STATE1 776
2011-04-21 08:26:26.000 2011-04-21 08:26:47.000 STATE2 21
2011-04-21 08:26:47.000 2011-04-21 08:27:30.000 STATE3 43
2011-04-21 08:27:30.000 2011-04-21 08:28:20.000 STATE1 50
2011-04-21 08:40:30.000 2011-04-21 08:41:00.000 STATE1 30
这里是例如SQL:
CREATE TABLE Data (
ID int IDENTITY(1,1) not null,
Date DateTime not null,
State nvarchar(40) not null,
Seconds int not null,
PRIMARY KEY(ID)
);
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:13:30.000', 'STATE1', 270)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:18:00.000', 'STATE1', 270)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:22:30.000', 'STATE1', 236)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:26:26.000', 'STATE2', 21)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:26:47.000', 'STATE3', 43)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:27:30.000', 'STATE1', 50)
INSERT INTO Data(Date,State,Seconds) VALUES('2011-04-21 08:40:30.000', 'STATE1', 30)
SELECT Date as 'start', DATEADD(s,Seconds,Date) as 'end', State, Seconds FROM Data
提前感谢!
完美!这是一个惊人的SQL。谢谢你的时间。 – Blazes
该解决方案似乎有一个错误。我已经改变了一些数据,如[这里]所示(https://gist.github.com/2205609)。然后当我运行你的答案的SQL时,我得到了不正确的结果。这似乎是因为您使用'a。[end] - ISNULL(b.start,a.start))'来划分最终结果,但这在不止一个组的情况下是不正确的,就像我的样品。 – VitalyB
5年后,从谷歌到这里,看不到'OVER(PARTITION by a。[end] - ISNULL(b.start,a.start))'可以工作 – OGHaza