要求是根据state-PASS对行进行编号,并按特定id的时间顺序排序:因此,当每次PASS发生在一个id的状态时,它应该对数字进行加密。如果没有发生传递,它应该保持数字
我目前正在尝试使用
rank() over (partition by id, user, state = 'PASS' order by time)
但这很有效,直到交替出现传球,但如果没有传球则不起作用
任何帮助将不胜感激。谢谢
这是样本数据:
ID user State time Req. number
-----------------------------------------------
1 a1 work timestamp1 1
1 f1 pass timestamp2 1
1 s1 work timestamp3 2
1 f1 pass timestamp4 2
1 m1 break timestamp5 3
1 s1 pass timestamp6 3
1 f1 work timestamp7 4
1 a1 pass timestamp8 4
1 v1 work timestamp9 5
1 s1 endwork timestamp10 5
1 s1 endwork timestamp11 5
1 a1 work timestamp12 5
2 a2 work timestamp13 1
2 s2 endwork timestamp14 1
答案 0 :(得分:2)
您似乎想要比“前一行”的累计“通过”数量多一个:
select t.*,
coalesce(sum(case when state = 'PASS' then 1 else 0 end) over
(partition by id
order by time
rows between unbounded preceding and 1 preceding
) + 1, 1
)
from t;