组连续日期范围来自同一个表SQL Server

时间:2014-12-14 16:44:01

标签: sql sql-server stored-procedures sql-server-2008-r2

我有以下数据:

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'

我需要按RateIdpropertyId分组以及连续日期范围。我用光标做了这个,但我不想要光标,因为我们有很多记录。

如果我们可以创建一个很棒的视图:)

感谢。

2 个答案:

答案 0 :(得分:2)

更改数据中所有20102015您可以预期的实际结果集

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+,则可以采用以下方法:

  • 查找与prev记录不重叠的所有记录。这些开始了一个范围。
  • 在任何给定记录之前计算此类记录的数量。这些为该范围分配一个常量值。
  • 将此作为分组因素使用。

查询如下:

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;