1
我有這樣的事情分組連續時間間隔
+------+-----+-----------+---------+
| Room | Day | StartTime | EndTime |
+------+-----+-----------+---------+
| 1 | 1 | 08:00 | 09:00 |
+------+-----+-----------+---------+
| 1 | 1 | 09:00 | 10:00 |
+------+-----+-----------+---------+
| 1 | 1 | 13:00 | 14:00 |
+------+-----+-----------+---------+
| 2 | 2 | 07:00 | 08:00 |
+------+-----+-----------+---------+
我想小組由房,日期和時間間隔,但只有連續的時間間隔,例如:
+------+-----+-----------+---------+
| Room | Day | StartTime | EndTime |
+------+-----+-----------+---------+
| 1 | 1 | 08:00 | 10:00 |
+------+-----+-----------+---------+
| 1 | 1 | 13:00 | 14:00 |
+------+-----+-----------+---------+
| 2 | 2 | 07:00 | 08:00 |
+------+-----+-----------+---------+
我有這個代碼,但我不滿意,因爲也是分組的空白,並拋出以下結果:
SELECT
sd.Cod_Room,
sd.Cod_Day,
MIN(bd.StartTime) as StartTime,
MAX(bd.EndTime) as EndTime
FROM
Schedule.ScheduleDetail AS sd
INNER JOIN Schedule.BlockDetail AS bd ON sd.Cod_BlockDetail = bd.Cod_BlockDetail
GROUP BY
sd.Room, sd.Day
+------+-----+-----------+---------+
| Room | Day | StartTime | EndTime |
+------+-----+-----------+---------+
| 1 | 1 | 08:00 | 14:00 |
+------+-----+-----------+---------+
| 2 | 2 | 07:00 | 08:00 |
+------+-----+-----------+---------+
我正在閱讀有關lead()和lag()的內容,但是這比我想象的要耗費更多的時間。 感謝您的幫助
您使用的是哪個版本的sql server? – JamieD77
@ JamieD77我正在使用2014 –