[英]Incremental values when a condition is met in if statement
我已經被困在這個問題上很長一段時間了,我無法弄清楚。 這是我的問題:我有兩個布爾列condition_1
和condition_2
,我想創建第三列inc
, if condition_2 is false and lead(condition_1) over(partition by column_x order by column_y) is false
每次此條件都會增加值遇到了。
結果看起來像這樣:
column_x column_y condition_1 condition_2 inc
A 12/03/2020 true true 1
A 13/03/2020 true false 1
A 14/03/2020 false false 2
A 15/03/2020 false true 3
A 16/03/2020 true false 3
A 17/03/2020 false true 4
做類似的事情
if(condition_2 is false and lead(condition_1) over(partition by column_x order by column_y) is false, lag(inc) over(partition by column_x order by column_y) + 1, lag(inc) over(partition by column_x order by column_y)) inc
obv 不起作用,因為inc
在查詢時尚不存在,並且正在執行
if(condition_2 is false and lead(condition_1) over(partition by column_x order by column_y) is false, + 1, + 0) inc
不會遞增,因為每行都會重置為 0。
有人有想法嗎?
非常感謝!
你描述這個公式:
select t.*,
countif( (not condition_2) and (not next_1)) over (partition by column_x order by column_y)
from (select t.*,
lead(condition_1) over (partition by column_x order by column_y) as next_1
from t
) t;
如果您希望數字從 1 開始,則需要將“1”添加到值中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.