在SQL Server 2008中连续合并连续记录
假设我有这样的数据,时间段基本上是相隔30分钟的时间。 请注意,在15:30至16:30之间,2021-12-24中存在差距。
CALENDER_DATE | TIMESLOT | TIMESLOT_END |
---|---|---|
2021-12-24 | 14:00:00 | 14:30:00 |
2021-12-24 | 14:30:00 | 15:00 15:00 |
2021-12-24 | 15:00 15:00 | 15:30:00 |
2021- 12-24 | 16:30:00 | 17:00:00 |
2021-12-24 | 17:00:00 | 17:30:00 |
2021-12-24 | 17:30:00 | 18:00:00 |
2021-12-30 | 09: 00:00 | 09:30:00 |
2021-12-30 | 09:30:00 | 10:00:00 |
我想合并行时lipslot_end = Next Row的时光段,并且在同一天,因此数据看起来像这样。
CALENDER_DATE | TIMESLOT | TIMESLOT_END |
---|---|---|
2021-12-24 | 14:00:00 | 15:30:00 |
2021-12-24 | 16:30:00 | 18:00:00 |
2021-12-30 | 9:00 | 10:00 |
我有尝试使用自连接的行编号,
WITH cte AS
(
SELECT
calender_date
,timeslot
,timeslot_end
,ROW_NUMBER()OVER(ORDER BY [timeslot])rn
FROM @tmp_leave tl
)
SELECT
MIN(a.timeslot) OVER(PARTITION BY a.calender_date, DATEDIFF(minute,a.timeslot_end,
ISNULL(b.timeslot, a.timeslot_end))) AS 'StartTime',
MAX(a.timeslot_end ) OVER(PARTITION BY a.calender_date,
DATEDIFF(minute,a.timeslot_end, ISNULL(b.timeslot, a.timeslot_end))) AS 'EndTime'
FROM cte a
LEFT JOIN cte b
ON a.rn + 1 = b.rn AND a.timeslot_end = b.timeslot
ORDER BY calender_date
但是结果不太正确,它忽略了2021-12-24中的差距,并在下面返回。
calender_date | timeslot | timeslot_end |
---|---|---|
2021-12-24 | 14:00:00 | 18:00:00 |
2021-12-30 | 9:00:00 | 10:00:00 |
我一直在搜索并尝试解决一段时间,请帮助,请帮助,任何帮助都非常感谢!!
Say I have data like this, timeslots is basically time with 30 mins apart.
Note there is a gap in 2021-12-24 between 15:30 and 16:30.
calender_date | timeslot | timeslot_end |
---|---|---|
2021-12-24 | 14:00:00 | 14:30:00 |
2021-12-24 | 14:30:00 | 15:00:00 |
2021-12-24 | 15:00:00 | 15:30:00 |
2021-12-24 | 16:30:00 | 17:00:00 |
2021-12-24 | 17:00:00 | 17:30:00 |
2021-12-24 | 17:30:00 | 18:00:00 |
2021-12-30 | 09:00:00 | 09:30:00 |
2021-12-30 | 09:30:00 | 10:00:00 |
I want to merge rows where timeslot_end = next row's timeslot and in the same day, so data would look like this.
calender_date | timeslot | timeslot_end |
---|---|---|
2021-12-24 | 14:00:00 | 15:30:00 |
2021-12-24 | 16:30:00 | 18:00:00 |
2021-12-30 | 9:00:00 | 10:00:00 |
I have try row numbering with self join,
WITH cte AS
(
SELECT
calender_date
,timeslot
,timeslot_end
,ROW_NUMBER()OVER(ORDER BY [timeslot])rn
FROM @tmp_leave tl
)
SELECT
MIN(a.timeslot) OVER(PARTITION BY a.calender_date, DATEDIFF(minute,a.timeslot_end,
ISNULL(b.timeslot, a.timeslot_end))) AS 'StartTime',
MAX(a.timeslot_end ) OVER(PARTITION BY a.calender_date,
DATEDIFF(minute,a.timeslot_end, ISNULL(b.timeslot, a.timeslot_end))) AS 'EndTime'
FROM cte a
LEFT JOIN cte b
ON a.rn + 1 = b.rn AND a.timeslot_end = b.timeslot
ORDER BY calender_date
But the result isn't quite right, it ignored the gap in 2021-12-24 and return below.
calender_date | timeslot | timeslot_end |
---|---|---|
2021-12-24 | 14:00:00 | 18:00:00 |
2021-12-30 | 9:00:00 | 10:00:00 |
I have been searching and trying to solve it for a while now, please help, any help is very very appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个差距和岛屿问题。我们可以通过为每个连续日期/时间创建伪群来解决这个问题。
amp
demo
This is a gaps and islands problem. We can approach this by creating pseudo groups for each island of continuous dates/times.
Demo