[英]BigQuery / Count the number of rows until a specific row is reached
我在 BigQuery 中有数据。
我想在每个 ID 的“批准”事件之前计算“待定”事件的数量。
我将如何获得每个个人 ID 的价值?
表事件
id event
1 pending
1 pending
1 pending
1 approved
2 pending
1 pending
1 pending
1 approved
2 approved
在这个例子中正确的结果是
id count_events
1 3
1 2
2 1
考虑以下方法
select id, countif(not flag) count_events
from (
select *, countif(flag) over(partition by id order by ts desc) grp
from (
select *,
if(lag(event) over(partition by id order by ts) = 'pending' and event = 'approved', true, false) flag
from your_table
)
)
group by id, grp
order by max(ts)
如果应用于您问题中的示例数据 - output 是
注意ts
的使用 - 你的表中必须有一些定义事件顺序的列 - 通常它是时间戳,但可以是日期,或者只是序号等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.