简体   繁体   English

BigQuery / 计算行数直到到达特定行

[英]BigQuery / Count the number of rows until a specific row is reached

I have data in BigQuery.我在 BigQuery 中有数据。

I want to count the number of 'pending' events before their 'approved' event per ID.我想在每个 ID 的“批准”事件之前计算“待定”事件的数量。

How would I get the value for every individual ID?我将如何获得每个个人 ID 的价值?

Table events表事件

id  event
1   pending
1   pending
1   pending
1   approved
2   pending
1   pending
1   pending
1   approved
2   approved

In this example the right result is在这个例子中正确的结果是

id  count_events
1   3
1   2
2   1

Consider below approach考虑以下方法

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)    

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

Note use of ts - you must have some column in your table that defines order of events - usually it is timestamp, but can be date, or just sequential number etc.注意ts的使用 - 你的表中必须有一些定义事件顺序的列 - 通常它是时间戳,但可以是日期,或者只是序号等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM