我有以下数据:
CREATE TABLE #Rate
(
RateId Bigint
,PropertyId Bigint
,StartDate DATETIME
,EndDate DATETIME
)
INSERT INTO #Rate VALUES (100,1000,'2015-01-01','2010-01-11')
INSERT INTO #Rate VALUES (100,1000,'2015-01-12','2015-02-02')
INSERT INTO #Rate VALUES (100,1000,'2015-02-11','2015-02-25')
INSERT INTO #Rate VALUES (100,1002,'2015-01-01','2010-01-11')
INSERT INTO #Rate VALUES (100,1002,'2015-01-12','2015-02-02')
INSERT INTO #Rate VALUES (101,1000,'2015-02-11','2015-02-25')
INSERT INTO #Rate VALUES (101,1000,'2015-01-01','2010-01-11')
INSERT INTO #Rate VALUES (101,1000,'2015-01-12','2015-02-02')
我需要这个结果集
100 1000 '2015-01-01' '2015-02-02'
100 1000 '2015-02-11' '2015-02-25'
100 1002 '2015-01-01' '2015-02-02'
101 1002 '2015-01-01' '2015-02-02'
我需要按RateId
和propertyId
分组以及连续日期范围。我用光标做了这个,但我不想要光标,因为我们有很多记录。
如果我们可以创建一个很棒的视图:)
感谢。
答案 0 :(得分:2)
更改数据中所有2010
和2015
您可以预期的实际结果集
RateId PropertyId StartDate EndDate
-------------------- -------------------- ---------- ----------
100 1000 2015-01-01 2015-02-02
100 1000 2015-02-11 2015-02-25
100 1002 2015-01-01 2015-02-02
101 1000 2015-01-01 2015-02-02
101 1000 2015-02-11 2015-02-25
这个问题与find start and stop date for contiguous dates in multiple rows非常相似,所以我会用我的答案作为模板
WITH D AS (
SELECT RateId, PropertyId, StartDate, EndDate
, _Id = ROW_NUMBER() OVER (PARTITION BY RateId, PropertyId
ORDER BY StartDate, EndDate)
FROM #Rate
), N AS (
SELECT m.RateId, m.PropertyId, m.StartDate, m.EndDate
, LastStop = p.EndDate
FROM D m
LEFT JOIN D p ON m.RateID = p.RateId
AND m.PropertyId = p.PropertyId
AND m._Id = p._Id + 1
), B AS (
SELECT RateId, PropertyId, StartDate, EndDate, LastStop
, Block = SUM(CASE WHEN LastStop Is Null Then 1
WHEN LastStop + 1 < StartDate Then 1
ELSE 0
END)
OVER (PARTITION BY RateId, PropertyId ORDER BY StartDate, EndDate)
FROM N
)
SELECT RateId, PropertyId
, MIN(StartDate) StartDate
, MAX(EndDate) EndDate
FROM B
GROUP BY RateId, PropertyId, Block
ORDER BY RateId, PropertyId, Block;
D
会生成一个行计数器,以避免使用三角形连接
N
获取每行中同一EndDate
组中的上一个RateID, PropertyID
B
为每个块生成序列号
主查询聚合B
中的数据以获取所需的结果集。
答案 1 :(得分:0)
假设您使用的是SQL Server 2012+,则可以采用以下方法:
查询如下:
select rateid, propertyid, min(startdate) as startdate, max(enddate) as enddate
from (select r.*,
sum(case when preved < startdate then 1 else 0 end) over (partition by rateid, propertyid order by startdate) as grp
from (select r.*,
lag(enddate) over (partition by rateid, propertyid order by enddate) as preved
from #Rate r
) r
) r
group by rateid, propertyid, grp;
编辑:
在SQL Server 2008中,您可以执行类似的操作:
with r as (
select r.*,
(case when exists (select 1
from #rate r2
where r2.rateid = r.rateid and r2.propertyid = r.propertyid and
(r2.startdate <= dateadd(1 day, r.enddate) and
r2.enddate >= r.startdate)
) then 0 else 1 end) as isstart
from #Rate r join
#Rate r2
)
select rateid, propertyid, min(startdate) as startdate, max(enddate) as enddate
from (select r.*,
(select sum(isstart)
from r r2
where r2.rateid = r.rateid and r2.propertyid = r.propertyid
r2.startdate <= r.startdate) as grp
from r
) r
group by rateid, propertyid, grp;