在SQL中组合行

时间:2015-03-31 09:33:39

标签: sql sql-server-2008

只要记录发生某些事情,我就会在SQL中记录开始和结束时间,基本上用户打开一个包含记录信息的屏幕,我想看看屏幕打开的时间。

我在类似于此的链接表中记录了开始时间,结束时案例当前案例的阶段:

| Id        | Reference   | Stage | StartTime                   | EndTime                     | 
| 1         | 123456789   | NEW   | 2015-03-30 16:04:39.8100000 | NULL                        |                  
| 2         | 123456789   | NEW   | NULL                        | 2015-03-30 16:06:44.3830000 |
| 3         | 123456789   | VAL   | 2015-03-30 16:09:00.3800000 | NULL                        |
| 4         | 123456789   | VAL   | NULL                        | 2015-03-30 16:11:04.9870000 |
| 5         | 123456789   | CAP   | 2015-03-30 16:20:25.7900000 | NULL                        |
| 6         | 123456789   | CAP   | NULL                        | 2015-03-30 16:21:12.1130000 |
| 7         | 123456789   | CAP   | 2015-03-30 16:22:40.4930000 | NULL                        |
| 8         | 123456789   | CAP   | NULL                        | 2015-03-30 16:29:02.5030000 |
| 9         | 123456789   | CAP   | 2015-03-30 16:29:17.8970000 | NULL                        |
| 10        | 123456789   | CAP   | NULL                        | 2015-03-30 16:55:13.8870000 |
| 11        | 123456789   | CAP   | 2015-03-30 16:56:20.2230000 | NULL                        |
| 12        | 123456789   | CAP   | NULL                        | 2015-03-30 16:56:40.3830000 |

我一直在玩,但无法获得理想的结果:

| Id        | Reference   | Stage | StartTime                   | EndTime                     | 
| 1         | 123456789   | NEW   | 2015-03-30 16:04:39.8100000 | 2015-03-30 16:06:44.3830000 |  
| 3         | 123456789   | VAL   | 2015-03-30 16:09:00.3800000 | 2015-03-30 16:11:04.9870000 |
| 5         | 123456789   | CAP   | 2015-03-30 16:20:25.7900000 | 2015-03-30 16:21:12.1130000 |
| 7         | 123456789   | CAP   | 2015-03-30 16:22:40.4930000 | 2015-03-30 16:29:02.5030000 |
| 9         | 123456789   | CAP   | 2015-03-30 16:29:17.8970000 | 2015-03-30 16:55:13.8870000 |
| 11        | 123456789   | CAP   | 2015-03-30 16:56:20.2230000 | 2015-03-30 16:56:40.3830000 |

在这种情况下,如何组合数据(开始和结束时间)?

3 个答案:

答案 0 :(得分:1)

您可以选择StartTime行并使用内部选择填充EndTime:

SELECT Id, Reference, Stage, StartTime, 
           (SELECT t1i.EndTime FROM Table1 t1i WHERE t1.Stage = t1i.Stage And t1i.StartTime IS NULL
           And t1i.Id = (SELECT min(t1ii.Id) FROM t1ii WHERE t1ii.StartTime IS NULL And t1ii.Stage = t1.Stage And t1ii.Id > t1.Id) )
FROM Table1 t1
WHERE t1.EndTime IS NULLL

答案 1 :(得分:1)

SELECT * FROM (
SELECT [Id], [Reference], [Stage], [StartTime],lead(EndTime) OVER (ORDER BY    ID) AS NewEndDate FROM yourTablename 
) tbl
WHERE id % 2 = 1

编辑:此查询适用于SQL Server 2012及更高版本

如果您使用的是旧版本的SQL Server,则可以使用以下查询:

SELECT * FROM (
SELECT t.[Id], t.[Reference], t.[Stage], t.[StartTime], tnext.[EndTime]
FROM dates t INNER JOIN
 dates tnext
 ON t.id = tnext.id - 1
) tbl
WHERE id % 2 = 1

答案 2 :(得分:0)

尝试类似:

SELECT MIN(id), reference, stage, MAX(startTime), MAX(endTime)
FROM mytable
GROUP BY reference, stage
ORDER BY id