我花了相当长的一段时间处理有N组的边界如下:识别
假设你有ň与多个记录每一个记录都有独特starting
和ending
点组的数目。
换句话说:
ID|GroupName|StartingPoint|EndingPoint|seq(row_number)|desired_seq
__|_________|_____________|___________|_______________|____________
1 | Grp1 |2014-01-06 |2014-01-07 |1 |1
__|_________|_____________|___________|_______________|____________
2 | Grp1 |2014-01-07 | 2014-01-08|2 |2
__|_________|_____________|___________|_______________|____________
3 | Grp2 |2014-01-08 | 2014-01-09|1 |1
__|_________|_____________|___________|_______________|____________
4 | Grp1 |2014-01-09 | 2014-01-10|3 |1
__|_________|_____________|___________|_______________|____________
5 | Grp2 |2014-01-10 | 2014-01-11|2 |1
__|_________|_____________|___________|_______________|____________
正如你所看到的,starting point
每一个连续的记录是相同以前的ending point
。
基本上,我想根据日期为每个组获得minimumS and maximumS
。一旦出现带有新组名称的记录,则将其视为新组并重置排序。
单row_number()
功能不是此任务足以因为它不反映在组名称的变化。(我已经包含在采样数据一SEQ列表示由行数所产生的值)
期望结果根据样本数据:
1 Grp1 |2014-01-06 | 2014-01-08
2 Grp2 |2014-01-08 | 2014-01-09
3 Grp1 |2014-01-09 | 2014-01-10
4 Grp2 |2014-01-10 | 2014-01-11
我曾尝试:
;with cte as(
select *
, row_number() over (partition by GroupName order by startingpoint) as seq
from table1
)
select *
into #temp2
from cte t1
left join cte t2 on t1.id=t2.id and t1.seq= t2.seq-1
select *
,(select startingPoint from #temp2 t2 where t1.id=t2.id and t2.seq= (select MIN(seq) from #temp2) as Oldest
(select startingPoint from #temp2 t2 where t1.id=t2.id and t2.seq= (select MAX(seq) from #temp2) as MostRecent
from #temp2 t1
从表格判断,似乎你可以使用'MIN'和'MAX',除非我失去了一些东西。 – Zane