考虑日期顺序的 SQL 组数据
Posted
技术标签:
【中文标题】考虑日期顺序的 SQL 组数据【英文标题】:SQL group data taking date sequence into account 【发布时间】:2020-02-19 22:04:14 【问题描述】:我正在尝试对可能被不同组破坏的单独行进行分组。如果它们坏了,我希望它们分开分组。
所以我有:
Col1 | Col2
---------------------
| Y |01/JAN/2012
| Y |01/FEB/2012
| N |01/MAR/2012
| Y |01/APR/2012
| Y |01/MAY/2012
我想得到结果:
|col1|col2 |GRP
---------------------
| Y |01/JAN/2012|1
| Y |01/FEB/2012|1
| N |01/MAR/2012|2
| Y |01/APR/2012|3
| Y |01/MAY/2012|3
我怎样才能做到这一点?
我目前的尝试是这样的:
select
Col1,
Col2,
dense_rank() over (partition by Col1 order by Col2 asc) as grp
from
myTABLE
;
但这会将所有的“Y”组合在一起并给我一个这样的序号:
|col1|col2 |GRP
---------------------
| Y |01/JAN/2012|1
| Y |01/FEB/2012|2
| N |01/MAR/2012|1
| Y |01/APR/2012|3
| Y |01/MAY/2012|4
【问题讨论】:
【参考方案1】:这是一种差距和孤岛问题。我建议使用行号的差异来识别“岛”,然后row_number()
:
select t.*, dense_rank() over (order by grp) as grp
from (select t.*,
min(col2) over (partition by col1, seqnum - seqnum_2) as grp
from (select t.*,
row_number() over (order by col2) as seqnum,
row_number() over (partition by col1 order by col2) as seqnum_2
from t
) t
) t
order by col2;
其实更简单的方法是使用lag()
和一个累计和:
select t.*,
sum(case when col1 = prev_col1 then 0 else 1 end) over (order by col2) as grp
from (select t.*, lag(col1) over (partition by col2) as prev_col1
from t
) t
【讨论】:
以上是关于考虑日期顺序的 SQL 组数据的主要内容,如果未能解决你的问题,请参考以下文章