TSQL:将连续时隙组合在一起 [英] TSQL: Grouping continuous timeslots together

查看:87
本文介绍了TSQL:将连续时隙组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


亲爱的社区,

Dear community,




希望你能帮我解决这个问题问题: 


hope you can help me solving this issue: 


我必须分组连续的 时隙在一起:

I have to group continuous timeslots together:


 

 


示例:


 

 


DECLARE @TEST as Table(ID int,tFrom datetime,tUntil dateTime)

插入@TEST值(1,'2019-1-1 12:00','2019-1 -1 13:00')

插入@TEST值(1,'2019-1-1 13:00','2019-1-1 14:00')

插入@TEST值(1,'2019-1-1 14:00','2019-1-1 16:00')

插入@TEST值(1,'2019-1-1 18:00','2019-1-1 19:00')

插入@TEST值(1,' 2019-1-1 19:00','2019-1-1 20:00')

插入@TEST值(1,'2019-1-1 20:00' ,'2019-1-1 21:00')

插入@TEST值(1,'2019-1-1 22:00','2019-1-1 23: 00')

插入@TEST值(2,'2019-1-1 12:00','2019-1-1 13:00')

插入@TEST值(2,'2019-1-1 13:00','2019-1-1 14:00')

插入@TEST值(2,'2019-1-1 14:00','2019-1-1 16:00')

插入@TEST值(2,'2019-1- 1 18:00','2019-1-1 19:00')

插入@TEST值(2,'2019-1-1 19:00','2019- 1-1 20:00 )

插入@TEST值(2,'2019-1-1 20:00','2019-1-1 21:00')

插入@TEST值(2,'2019-1-1 22:00','2019-1-1 23:00')

DECLARE @TEST as Table (ID int, tFrom datetime, tUntil dateTime)
insert into @TEST Values (1,'2019-1-1 12:00', '2019-1-1 13:00')
insert into @TEST Values (1,'2019-1-1 13:00', '2019-1-1 14:00')
insert into @TEST Values (1,'2019-1-1 14:00', '2019-1-1 16:00')
insert into @TEST Values (1,'2019-1-1 18:00', '2019-1-1 19:00')
insert into @TEST Values (1,'2019-1-1 19:00', '2019-1-1 20:00')
insert into @TEST Values (1,'2019-1-1 20:00', '2019-1-1 21:00')
insert into @TEST Values (1,'2019-1-1 22:00', '2019-1-1 23:00')
insert into @TEST Values (2,'2019-1-1 12:00', '2019-1-1 13:00')
insert into @TEST Values (2,'2019-1-1 13:00', '2019-1-1 14:00')
insert into @TEST Values (2,'2019-1-1 14:00', '2019-1-1 16:00')
insert into @TEST Values (2,'2019-1-1 18:00', '2019-1-1 19:00')
insert into @TEST Values (2,'2019-1-1 19:00', '2019-1-1 20:00')
insert into @TEST Values (2,'2019-1-1 20:00', '2019-1-1 21:00')
insert into @TEST Values (2,'2019-1-1 22:00', '2019-1-1 23:00')


 

 


预期结果:

Expected result:


 

 


1;  2019-1-1 12:00;  2019-1-1 16:00

1; 2019-1-1 12:00; 2019-1-1 16:00


1;  2019-1-1 18:00;  2019-1-1 21:00

1; 2019-1-1 18:00; 2019-1-1 21:00


1;  2019-1-1 22:00;  2019-1-1 23:00

1; 2019-1-1 22:00; 2019-1-1 23:00


 

 


2;  2019-1-1 12:00;  2019-1-1 16:00

2; 2019-1-1 12:00; 2019-1-1 16:00


2;  2019-1-1 18:00;  2019-1-1 21:00

2; 2019-1-1 18:00; 2019-1-1 21:00


2;  2019-1-1 22:00;  2019-1-1 23:00

2; 2019-1-1 22:00; 2019-1-1 23:00


 

 


如果能提供帮助,你会这么好吗

Would you so great if you can help

推荐答案

你好tommired2000,

Hi tommired2000,

 

请尝试按照以下脚本查看它是否满足您的要求。

Please try following script to see if it satisfies your requirement.

DECLARE @TEST as Table (ID int, tFrom datetime, tUntil dateTime)
insert into @TEST Values (1,'2019-1-1 12:00', '2019-1-1 13:00')
insert into @TEST Values (1,'2019-1-1 13:00', '2019-1-1 14:00')
insert into @TEST Values (1,'2019-1-1 14:00', '2019-1-1 16:00')
insert into @TEST Values (1,'2019-1-1 18:00', '2019-1-1 19:00')
insert into @TEST Values (1,'2019-1-1 19:00', '2019-1-1 20:00')
insert into @TEST Values (1,'2019-1-1 20:00', '2019-1-1 21:00')
insert into @TEST Values (1,'2019-1-1 22:00', '2019-1-1 23:00')
insert into @TEST Values (2,'2019-1-1 12:00', '2019-1-1 13:00')
insert into @TEST Values (2,'2019-1-1 13:00', '2019-1-1 14:00')
insert into @TEST Values (2,'2019-1-1 14:00', '2019-1-1 16:00')
insert into @TEST Values (2,'2019-1-1 18:00', '2019-1-1 19:00')
insert into @TEST Values (2,'2019-1-1 19:00', '2019-1-1 20:00')
insert into @TEST Values (2,'2019-1-1 20:00', '2019-1-1 21:00')
insert into @TEST Values (2,'2019-1-1 22:00', '2019-1-1 23:00')

;with cte as (
select * from @TEST a 
where not exists (select * from @TEST b where b.tUntil=a.tFrom))
,cte1 as (
select *,1 as level from cte 
union all 
select  a.id,a.tFrom,b.tUntil,level+1 as level from cte1 a join @TEST b on a.tUntil=b.tFrom and a.ID=a.ID
)
select distinct id,CONVERT(varchar(16),tFrom,121) as tFrom,CONVERT(varchar(16),max(tUntil)over(partition by id,tFrom),121) as tUntil
from cte1
/*
id          tFrom            tUntil
----------- ---------------- ----------------
1           2019-01-01 12:00 2019-01-01 16:00
1           2019-01-01 18:00 2019-01-01 21:00
1           2019-01-01 22:00 2019-01-01 23:00
2           2019-01-01 12:00 2019-01-01 16:00
2           2019-01-01 18:00 2019-01-01 21:00
2           2019-01-01 22:00 2019-01-01 23:00
*/


;with cte as (
select * from @TEST a 
where not exists (select * from @TEST b where b.tUntil=a.tFrom))
,cte1 as (
select *,1 as level from cte 
union all 
select  a.id,a.tFrom,b.tUntil,level+1 as level from cte1 a join @TEST b on a.tUntil=b.tFrom and a.ID=a.ID
)
select distinct cast(id as varchar(10))+';'+ CONVERT(varchar(16),tFrom,121)+';'+CONVERT(varchar(16),max(tUntil)over(partition by id,tFrom),121) as result
from cte1
/*
result
--------------------------------------------
1;2019-01-01 12:00;2019-01-01 16:00
1;2019-01-01 18:00;2019-01-01 21:00
1;2019-01-01 22:00;2019-01-01 23:00
2;2019-01-01 12:00;2019-01-01 16:00
2;2019-01-01 18:00;2019-01-01 21:00
2;2019-01-01 22:00;2019-01-01 23:00
*/




希望它可以帮到你。


Hope it can help you.

 

最好的问候,

Rachel


这篇关于TSQL:将连续时隙组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆