我使用T-SQL(Microsoft SQL Server管理工作室2017年),我有类似于下面的数据集:
BEGINDATE ENDDATE ID
2015-07-01 2015-07-12 1
2015-07-01 2015-07-12 1
2015-07-11 2015-07-15 1
2015-07-18 2015-08-04 1
2015-06-28 2015-07-04 2
2015-06-28 2015-07-03 2
2015-06-29 2015-07-04 2
2015-07-03 2015-07-15 2
2015-07-17 2015-07-20 2
我想这样做是ID合并重叠的时间(有迹象表明,这样做,但不按组的几个例子在那里- 像这样的 )。
理想情况下,最终的结果会是这样的:
BEGINDATE ENDDATE ID
2015-07-01 2015-07-15 1
2015-07-18 2015-08-04 1
2015-06-28 2015-07-15 2
2015-07-17 2015-07-20 2
有什么建议么?
使用即席日历表的差距和离岛的解决方案:
declare @fromdate date, @thrudate date;
select @fromdate = min(begindate), @thrudate = max(enddate) from t;
;with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, dates as (
select top (datediff(day, @fromdate, @thrudate)+1)
[Date]=convert(date,dateadd(day,row_number() over(order by (select 1))-1,@fromdate))
from n as deka cross join n as hecto cross join n as kilo
cross join n as tenK cross join n as hundredK
order by [Date]
)
, cte as (
select
t.id
, d.date
, grp = row_number() over (partition by t.id order by d.date)
- datediff(day,@fromdate,d.date)
from dates d
inner join t
on d.date >= t.begindate
and d.date <= t.enddate
group by t.id, d.date
)
select
BeginDate = min(cte.date)
, EndDate = max(cte.date)
, id
from cte
where id is not null
group by id, grp
order by id, BeginDate
rextester演示: http://rextester.com/NKGY7104
收益:
+------------+------------+----+
| BeginDate | EndDate | id |
+------------+------------+----+
| 2015-07-01 | 2015-07-15 | 1 |
| 2015-07-18 | 2015-08-04 | 1 |
| 2015-06-28 | 2015-07-15 | 2 |
| 2015-07-17 | 2015-07-20 | 2 |
+------------+------------+----+