我有一种情况,我需要查找值更改之间的时间跨度。我尝试了一个简单的group by子句,但它消除了重叠的更改。考虑以下示例:
create table #items ( code varchar(4) , class varchar(4) , txdate datetime ) insert into #items (code, class, txdate) values ('A', 'C', '2010-01-01'); insert into #items (code, class, txdate) values ('A', 'C', '2010-01-02'); insert into #items (code, class, txdate) values ('A', 'C', '2010-01-03'); insert into #items (code, class, txdate) values ('A', 'D', '2010-01-04'); insert into #items (code, class, txdate) values ('A', 'D', '2010-01-05'); insert into #items (code, class, txdate) values ('A', 'C', '2010-01-06'); insert into #items (code, class, txdate) values ('A', 'C', '2010-01-07'); insert into #items (code, class, txdate) values ('A', 'D', '2010-01-08'); insert into #items (code, class, txdate) values ('A', 'D', '2010-01-09'); select code , class , min(txdate) mindate , max(txdate) maxdate from #items group by code, class
这将返回以下结果(请注意重叠的日期范围):
|code|class|mindate |maxdate | ---------------------------------- |A |C |2010-01-01|2010-01-07| |A |D |2010-01-04|2010-01-09|
我希望查询返回以下内容:
|code|class|mindate |maxdate | ---------------------------------- |A |C |2010-01-01|2010-01-03| |A |D |2010-01-04|2010-01-05| |A |C |2010-01-06|2010-01-07| |A |D |2010-01-08|2010-01-09|
有什么想法和建议吗?
SQL SERVER ISLANDS按照@KM的建议进行研究后,我想到了以下查询,当向数据集中添加其他类代码时,此查询似乎运行良好。
SQL SERVER ISLANDS
select a.code, a.class, a.txdate as mindate, b.txdate as maxdate from ( --Find minimum island select code , class , txdate , row_number() over (order by code, class, txdate) as n from #items tb1 where not exists ( select * from #items tb2 where datediff(d, tb1.txdate, tb2.txdate) = -1 and tb1.class = tb2.class and tb1.code = tb2.code ) ) as a inner join ( --Find maximum island select code , class , txdate , row_number() over (order by code, class, txdate) as n from #items tb1 where not exists ( select * from #items tb2 where datediff(d, tb1.txdate, tb2.txdate) = 1 and tb1.class = tb2.class and tb1.code = tb2.code ) ) as b on a.n = b.n
此方法的唯一警告是,最小集合中的条目数需要与最大集合中的条目数匹配。到目前为止,我还无法做任何使这不成立的事情。但是,我没有测试空值或性能。