![](/img/trans.png)
[英]Query that counts total records per day and total records with same time timestamp and id per day in Bigquery
[英]BigQuery: counts iterations per work block
我需要一个BIGQUERY
查询,它可以计算每个工作块的迭代次数。
当它“完成”时,一个工作块将完成。
我有这张桌子:
version_id status
ghfjghj pending
gsffsg pending
jhkukiul done
fsgsfgffgsf pending
nmyumuju done
我想要这个结果:
block iterations
1 3
2 2
您可以计算达到每个新的完成状态所需的状态数。 如果您有 PK 或某些唯一 ID 列,它就可以工作。 样本数据:
with cte as (
select 1 as id,'pending' as status
union all select 2,'pending'
union all select 3, 'done'
union all select 4,'pending'
union all select 5, 'done'
)
枚举每个完成状态(块)和所有状态(rn)。
,
cte2 as (
select
row_number () over (partition by status order by id) as block,
row_number () over (order by id) as rn,
id,status
from cte)
计算这个和之前的 Done 状态之间的索引差异,为每个 Done 状态找到直到完成的其他状态的数量,这似乎是迭代次数。
select
block,
rn-lag(rn,1,0) over (order by block) as iteration
from cte2
where status = 'done'
order by 1
小提琴小提琴适用于 T-SQL,但也适用于 Bigquery SQL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.