tSQL确定多个日期范围(SQL Server 2000)



我有一个表,其中包含一个事件的IDDate。每行表示一个日期。我正试图确定连续的日期范围并合并输出以显示ID,StartDate,EndDate

ID      Date
200236  2011-01-02 00:00:00.000
200236  2011-01-03 00:00:00.000
200236  2011-01-05 00:00:00.000
200236  2011-01-06 00:00:00.000
200236  2011-01-07 00:00:00.000
200236  2011-01-08 00:00:00.000
200236  2011-01-09 00:00:00.000
200236  2011-01-10 00:00:00.000
200236  2011-01-11 00:00:00.000
200236  2011-01-12 00:00:00.000
200236  2011-01-13 00:00:00.000
200236  2011-01-15 00:00:00.000
200236  2011-01-16 00:00:00.000
200236  2011-01-17 00:00:00.000

输出如下:

ID       StartDate    EndDate
200236   2011-01-02   2011-01-03
200236   2011-01-05   2011-01-13
200236   2011-01-15   2011-01-17

在SQL Server 2000中如何处理这个问题?

SELECT ...
FROM   ...
WHERE  date_column BETWEEN '2011-01-02' AND '2011-01-15'
也许

?<一口>引用

或者您可以执行子查询并使用MAX链接下一条记录,其中日期为<=当前日期:

SELECT id, date, (SELECT MAX(date) FROM mytable WHERE date <= mytable.date) AS nextDate
FROM   mytable

或使用:

SELECT TOP 1 date
FROM         mytable
WHERE        date <= mytable.date AND id <> mytable.id
ORDER BY     date

作为子查询,因此它获取当前记录之后的下一个日期。

我刚刚在SQL Server 2008中做了类似的事情。我认为下面的翻译将适用于SQL Server 2000:

-- Create table variable
DECLARE @StartTable TABLE
(
  rowid INT IDENTITY(1,1) NOT NULL,
  userid int,
  startDate date
)
Insert Into @StartTable(userid, startDate)
--This finds the start dates by finding unmatched values
SELECT t1.ID, t1.[Date]
FROM Example As t1
LEFT OUTER JOIN Example As t2 ON t1.ID=t2.ID 
   And DateAdd(day, 1, t2.[Date]) = t1.[Date]
WHERE t2.[Date] Is NULL
ORDER BY t1.ID, t1.[Date]
-- Create table variable
DECLARE @EndTable TABLE
(
  rowid INT IDENTITY(1,1) NOT NULL,
  userid int,
  endDate date
)
Insert Into @EndTable(userid, endDate)
--This finds the end dates by getting unmatched values 
SELECT t1.ID, t1.[Date]
FROM Example As t1
LEFT OUTER JOIN Example As t2 ON t1.ID=t2.ID
   And DateAdd(day, -1, t2.[Date]) = t1.[Date]
WHERE t2.[Date] IS NULL
ORDER BY t1.ID, t1.[Date]
Select eT.userid, startDate, endDate 
From @EndTable eT
INNER JOIN @StartTable sT On eT.userid = sT.userid 
AND eT.rowid = sT.rowid;

因此,如您所见,我创建了两个表变量,一个用于开始,一个用于结束,方法是在[date]列中的日期之前或之后自连接表。这意味着对于开始表,我只选择之前没有日期的记录(因此这些记录将位于一个周期的开始),而对于结束表,我只选择后面没有日期的记录(因此这些记录将位于一个周期的结束)。

当这些被插入到表变量中时,由于Identity列,它们被按顺序编号。然后将两个表变量连接在一起。因为它们是有序的,所以开始日期和结束日期应该总是正确匹配。

这个解决方案适用于我,因为我每天每个ID最多有一个记录,我只对天感兴趣,而不是小时等。尽管它有几个步骤,但我喜欢它,因为它在概念上很简单,并且在没有游标或循环的情况下消除了匹配的记录。我希望它对你也有用。

这个问题可能对你有帮助。我直接引用了Rob Farley的回答,因为我觉得这是一个类似的问题。

您可以采用的一种方法是添加一个字段,该字段指示序列中的下一个日期。(要么将其添加到当前表中,要么使用临时表,将底层数据存储到临时表中,然后按顺序更新下一个日期)

你的初始数据结构看起来像这样:

ID, PerfDate, NextDate
200236, 2011-01-02, 2011-01-03
200236, 2011-01-03, 2011-01-04
etc.

然后,您可以使用一系列相关的子查询将数据滚动到所需的输出:

SELECT ID, StartDate, EndDate
FROM (
SELECT DISTINCT ID, PerfDate AS StartDate, 
    (SELECT MIN([PerfDate]) FROM [SourceTable] S3
    WHERE S3.ID = S1.ID
    AND S3.NextDate > S1.PerfDate
    AND ISNULL(
        (SELECT MIN(PerfDate) 
        FROM [SourceTable] AS S4
        WHERE S4.ID = S1.ID 
        AND S4.NextDate > S3.NextDate), S3.NextDate + 1) > S3.NextDate) AS EndDate
FROM [SourceTable] S1
WHERE 
    ISNULL(
        (SELECT MAX(NextDate) 
        FROM [SourceTable] S2 
        WHERE S2.ID = S1.ID 
        AND S2.PerfDate < S1.PerfDate), PerfDate -1) < S1.PerfDate)q
ORDER BY q.ID, q.StartDate

这是我过去的做法。这是一个两步的过程:

  1. 构建候选连续周期集
  2. 如果有重叠的时间段,删除除最长时间段外的所有时间段。

下面的脚本显示了如何完成。你也许能够在一个[bug,丑陋的]查询中完成它,但试图这样做会让我头疼。我使用临时表,因为它使整个调试容易得多。

drop table #source
create table #source
(
  id    int      not null ,
  dtCol datetime not null ,
  -----------------------------------------------------------------------
  -- ASSUMPTION 1: Each date must be unique for a given ID value.
  -----------------------------------------------------------------------
  unique clustered ( id , dtCol ) ,
  -----------------------------------------------------------------------
  -- ASSUMPTION 2: The datetime column only represents a day.
  -- The value of the time component is always 00:00:00.000
  -----------------------------------------------------------------------
  check ( dtCol = convert(datetime,convert(varchar,dtCol,112),112) ) ,
)
go
insert #source values(1,'jan 1, 2011')
insert #source values(1,'jan 4, 2011')
insert #source values(1,'jan 5, 2011')
insert #source values(2,'jan 1, 2011')
insert #source values(2,'jan 2, 2011')
insert #source values(2,'jan 3, 2011')
insert #source values(2,'jan 5, 2011')
insert #source values(3,'jan 1, 2011')
insert #source values(4,'jan 1, 2011')
insert #source values(4,'jan 2, 2011')
insert #source values(4,'jan 3, 2011')
insert #source values(4,'jan 4, 2011')
go
insert #source values( 200236 , '2011-01-02')
insert #source values( 200236 , '2011-01-03')
insert #source values( 200236 , '2011-01-05')
insert #source values( 200236 , '2011-01-06')
insert #source values( 200236 , '2011-01-07')
insert #source values( 200236 , '2011-01-08')
insert #source values( 200236 , '2011-01-09')
insert #source values( 200236 , '2011-01-10')
insert #source values( 200236 , '2011-01-11')
insert #source values( 200236 , '2011-01-12')
insert #source values( 200236 , '2011-01-13')
insert #source values( 200236 , '2011-01-15')
insert #source values( 200236 , '2011-01-16')
insert #source values( 200236 , '2011-01-17')
go
drop table #candidate_range
go
create table #candidate_range
(
  rowId   int      not null identity(1,1) ,
  id      int      not null ,
  dtFrom  datetime not null ,
  dtThru  datetime not null ,
  length  as 1+datediff(day,dtFrom,dtThru) ,
  primary key nonclustered ( rowID ) ,
  unique clustered (id,dtFrom,dtThru) ,
)
go
--
-- seed the candidate range table with the set of all possible contiguous ranges for each id
--
insert #candidate_range ( id , dtFrom , dtThru )
select id      = tFrom.id    ,
       valFrom = tFrom.dtCol ,
       valThru = tThru.dtCol
from #source tFrom
join #source tThru on tThru.id     = tFrom.id
                  and tThru.dtCol >= tFrom.dtCol
where 1+datediff(day,tFrom.dtCol,tThru.dtCol) = ( select count(*)
                                                  from #source t
                                                  where t.id = tFrom.id
                                                    and t.dtCol between tFrom.dtCol and tThru.dtCol
                                                )
order by 1,2,3
go
--
-- compare the table to itself. If we find overlapping periods,
-- we'll keep the longest such period and delete the shorter overlapping periods.
--
delete t2
from #candidate_range t1
join #candidate_range t2 on t2.id      = t1.id
                        and t2.rowId  != t1.rowID
                        and t2.length <  t1.length
                        and t2.dtFrom <= t1.dtThru
                        and t2.dtThru >= t1.dtFrom
go

相关内容

  • 没有找到相关文章

最新更新