大查询多次滚动计数
Big Query Multiple Rolling Count
我有这些数据
我想做两次滚动计数:
- 滚动计算每个(id,refund)。当变成不同的(id,refund)组合时(即使id相同),也会从0
开始
- 按每个 id 滚动计数。
我希望输出如下:
你能帮我创建查询吗?
我尝试了这个但是失败了...
SELECT
date,
id,
refund,
COUNT(CONCAT(id,refund)) OVER (PARTITION BY rn) AS count_id_refund,
COUNT(id) OVER (PARTITION BY rn) AS count_id
FROM table1
您可以使用 row_number() 函数。这是我的一些示例数据查询
with sample as (
select 1 as id, TRUE as refund,
union all select 1, true
union all select 1, false
union all select 2,true
union all select 3,true
union all select 3, false
union all select 3, false
)
select id,refund,
row_number() over(partition by id order by id) as row_id,
row_number() over(partition by concat(id,refund) order by concat(id,refund)) as row_id_rf
from sample order by id,refund desc;
结果是:
我有这些数据
- 滚动计算每个(id,refund)。当变成不同的(id,refund)组合时(即使id相同),也会从0 开始
- 按每个 id 滚动计数。
我希望输出如下:
你能帮我创建查询吗? 我尝试了这个但是失败了...
SELECT
date,
id,
refund,
COUNT(CONCAT(id,refund)) OVER (PARTITION BY rn) AS count_id_refund,
COUNT(id) OVER (PARTITION BY rn) AS count_id
FROM table1
您可以使用 row_number() 函数。这是我的一些示例数据查询
with sample as (
select 1 as id, TRUE as refund,
union all select 1, true
union all select 1, false
union all select 2,true
union all select 3,true
union all select 3, false
union all select 3, false
)
select id,refund,
row_number() over(partition by id order by id) as row_id,
row_number() over(partition by concat(id,refund) order by concat(id,refund)) as row_id_rf
from sample order by id,refund desc;
结果是: