如果在几周之间休息超过2周,我希望在几周内将我的行分成1组。
我有我的桌子:
Create table test.walk (animal text, week integer)
我想要分组的每个步行的行:
insert into test.walk values ('DOG', 2)
insert into test.walk values ('DOG', 3)
insert into test.walk values ('DOG', 4)
insert into test.walk values ('CAT', 1)
insert into test.walk values ('CAT', 1)
insert into test.walk values ('CAT', 11)
insert into test.walk values ('CAT', 12)
insert into test.walk values ('CAT', 13)
我在使用dense_rank()和lag()窗口函数时很困难,但是没有运气可以获得额外的列,因此我应该获得3个不同的值。
这是我想要的输出:
我应该使用哪种窗口函数组合来获取CAT的两个WALK_NO? (因为在第1周之后,猫等了2个星期才更长时间)
答案 0 :(得分:1)
使用lag()
和累积总和:
with query as (
select w.*,
sum(case when week < prev_week + 2 then 0 else 1 end) over (partition by animal order by week) as grp
from (select w.*,
lag(week) over (partition by animal order by week) as prev_week
from test.walk w
) w
)
select
animal, week, grp,
dense_rank() over (order by animal, grp) as grp2
from query
注意:这将重新启动每只动物的计数 - 这似乎是您想要做的事情的实际意图。如果你想要用动物分开的组,但是要增量,问题的定义有点棘手。一种方法是:
select w.*,
sum(case when prev_week = week then 0 else 1 end) over (order by min_week, animal, week) as grp
from (select w.*,
lag(week) over (partition by animal order by week) as prev_week,
min(week) over (partition by animal) as min_week
from test.walk w
) w;