22,207
社区成员
发帖
与我相关
我的任务
分享
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([student_id] int,[begin_date] Date,[end_date] Date)
Insert #T
select 1001,'2018-01-01','2018-01-15' union all
select 1001,'2018-01-15','2018-02-10' union all
select 1001,'2018-02-11','2018-03-20' union all
select 1002,'2018-01-01','2018-01-15' union all
select 1002,'2018-02-01','2018-03-10' union all
select 1002,'2018-03-09','2018-03-20'
Go
--测试数据结束
;WITH ctea AS (
Select *,ROW_NUMBER()OVER(PARTITION BY student_id ORDER BY end_date) rn from #T
),cteb AS (
SELECT a.student_id ,
a.begin_date ,
a.end_date ,
DATEDIFF(DAY, a.end_date, b.begin_date) AS diff
FROM ctea a
LEFT JOIN ctea b
ON b.student_id = a.student_id
AND b.rn = a.rn + 1)
,ctec AS (
SELECT cteb.student_id,
MIN(cteb.begin_date) AS begin_date,
MAX(cteb.end_date) AS end_date
FROM cteb
WHERE ISNULL(cteb.diff,0) < 3
GROUP BY cteb.student_id
UNION
SELECT cteb.student_id,
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([student_id] int,[begin_date] Date,[end_date] Date)
Insert #T
select 1001,'2018-01-01','2018-01-15' union all
select 1001,'2018-01-15','2018-02-10' union all
select 1001,'2018-02-11','2018-03-20' union all
select 1002,'2018-01-01','2018-01-15' union all
select 1002,'2018-02-01','2018-03-10' union all
select 1002,'2018-03-09','2018-03-20'
Go
--测试数据结束
;WITH ctea AS (
Select *,ROW_NUMBER()OVER(PARTITION BY student_id ORDER BY end_date) rn from #T
),cteb AS (
SELECT a.student_id ,
a.begin_date ,
a.end_date ,
DATEDIFF(DAY, a.end_date, b.begin_date) AS diff
FROM ctea a
LEFT JOIN ctea b
ON b.student_id = a.student_id
AND b.rn = a.rn + 1)
,ctec AS (
SELECT cteb.student_id,
MIN(cteb.begin_date) AS begin_date,
MAX(cteb.end_date) AS end_date
FROM cteb
WHERE cteb.diff < 3
GROUP BY cteb.student_id
UNION
SELECT cteb.student_id,
cteb.begin_date,
if OBJECT_ID(N'tempdb.dbo.#T') is not null
drop table #T
go
create table #T
(student_id varchar(5),
begin_date date,
end_date date)
insert into #T
select '1001','2018-01-01','2018-01-15' union all
select '1001','2018-01-20','2018-02-10' union all
select '1001','2018-02-15','2018-03-20' union all
select '1002','2018-01-01','2018-01-15' union all
select '1002','2018-01-16','2018-03-10' union all
select '1002','2018-03-15','2018-03-20' union all
select '1002','2018-03-21','2018-03-21' union all
select '1002','2018-03-25','2018-03-30'
with cte_1
as
(select *,ROW_NUMBER() over (partition by student_id order by begin_date) as seq from #T),
cte_2
as
(select A.*,isnull(DATEDIFF(DAY,B.end_date,A.begin_date),0) as diff
from cte_1 A
left join cte_1 B on A.student_id=B.student_id and A.seq=B.seq+1
),
cte_3
as
(select * from cte_2 where ABS(diff)>3 or seq=1)
select A.student_id,A.begin_date,C.end_date,ROW_NUMBER() over (partition by A.student_id order by A.begin_date) as rn
from cte_3 as A
outer apply
(select top 1 * from cte_3 where A.student_id=student_id and seq>A.seq order by seq) as B
outer apply
(select top 1 *
from cte_2 where student_id=A.student_id
and ((ISNULL(B.student_id,'')<>'' and seq=B.seq-1)
or ((ISNULL(B.student_id,'')='' and seq>=A.seq)))
order by seq desc) as C