Redshift:构建可变日期范围内的累计和
Redshift: Construct a cumulative sum over a variable date range
我正在努力构建一个使用日期范围动态构造累积总和的查询。
打个比方,我想计算每位客人每天平均订购的客房服务盘数。采用以下示例数据集:
guest_id
most_recent_plate_ordered_date
cumulative_plates_ordered
1
10/1/2020
1
1
10/2/2020
2
1
10/4/2020
3
2
10/1/2020
1
2
10/2/2020
1
3
10/3/2020
1
3
10/4/2020
2
这是我想要实现的期望输出:
date
cumulative_plates_ordered
number_of_people
10/1/2020
2
2
10/2/2020
3
2
10/3/2020
4
3
10/4/2020
6
3
本质上,我需要建立两个数字:每人最大订购盘数和每天人数之和。我已经生成了每天的人数——这很容易。我苦苦挣扎的地方是构建一个查询,该查询可以随着日期范围的扩大而动态求和。
我能够生成查询,为我提供给定日期最大值所需的数字。我的问题是将其转换为在一次查询中跨所有可能日期生成此数字的内容。下面是一个范围从 10/1 到 10/1 的查询示例:
select sum(max_cumulative_plates_ordered) from (
select guest_id, max(cumulative_plates_ordered) as max_cumulative_plates_ordered
from raw_data
where most_recent_plate_ordered_date <= '2020-10-01'
group by 1
)
有什么想法吗?感觉这个问题好难回答。
I was able to generate the query that gives me the desired number for a given date max. My problem is translating this into something that generates this number across all possible dates in one query
不只是想要 group by
子句中的日期?
select dt,
sum(cumulative_plates_ordered) as cumulative_plates_ordered,
count(*) as number_of_people
from (
select guest_id,
most_recent_plate_ordered_date::date as dt,
max(cumulative_plates_ordered) as cumulative_plates_ordered
from raw_data
group by 1,2
) t
group by dt
编辑
如果你想把“缺失”的日期记入账户,那就有点不同了。您可以使用 cross join
生成天数和客人的所有可能组合。然后用window函数补缺:
select dt,
sum(cumulative_plates_ordered) as cumulative_plates_ordered,
count(*) as number_of_people
from (
select g.guest_id, d.dt,
max(max(t.cumulative_plates_ordered)) over(order by d.dt) as cumulative_plates_ordered
from (select distinct most_recent_plate_ordered_date::date as dt from raw_data) d
cross join (select distinct guest_id from raw_data) g
left join raw_data t
on t.guest_id = g.guest_id
and t.most_recent_plate_ordered_date >= d.dt
and t.most_recent_plate_ordered_date < d.dt + interval 1 day
group by g.guest_id, d.dt
) t
group by dt
如果我没理解错的话,你想要:
- distinct 人在特定日期订购的人数。
- 当日最大订单数
cumulative_plates_ordered
的总和
然而,这表明 2020-10-03 的值实际上是 4 而不是 5。
一种方法是关联子查询:
select dte::date,
(select count(distinct guest_id)
from t
where t.most_recent_place_ordered <= gs.dte
) as num_guests,
(select sum(plates)
from (select t.guest_id, max(t.cumulative_plates_ordered) as plates
from t
where most_recent_place_ordered <= gs.dte
group by t.guest_id
) t
) as num_plates
from (select distinct most_recent_place_ordered as dte from t) gs;
使您的数据具有挑战性的是累积总和。您可以使用 lag()
获取特定日期的更改。有了这些数据,使用 window 函数和聚合得到你想要的结果就简单多了:
with net as (
select t.*,
row_number() over (partition by guest_id order by most_recent_place_ordered) as seqnum,
cumulative_plates_ordered - coalesce(lag(cumulative_plates_ordered) over (partition by guest_id order by most_recent_place_ordered), 0) as new_plates
from t
)
select most_recent_place_ordered,
sum(sum( (seqnum = 1)::int )) over (order by most_recent_place_ordered rows between unbounded preceding and current row) as num_guests,
sum(sum( new_plates )) over (order by most_recent_place_ordered rows between unbounded preceding and current row) as num_plates
from net
group by most_recent_place_ordered
order by most_recent_place_ordered;
Here 是一个 db<>fiddle.
我正在努力构建一个使用日期范围动态构造累积总和的查询。
打个比方,我想计算每位客人每天平均订购的客房服务盘数。采用以下示例数据集:
guest_id | most_recent_plate_ordered_date | cumulative_plates_ordered |
---|---|---|
1 | 10/1/2020 | 1 |
1 | 10/2/2020 | 2 |
1 | 10/4/2020 | 3 |
2 | 10/1/2020 | 1 |
2 | 10/2/2020 | 1 |
3 | 10/3/2020 | 1 |
3 | 10/4/2020 | 2 |
这是我想要实现的期望输出:
date | cumulative_plates_ordered | number_of_people |
---|---|---|
10/1/2020 | 2 | 2 |
10/2/2020 | 3 | 2 |
10/3/2020 | 4 | 3 |
10/4/2020 | 6 | 3 |
本质上,我需要建立两个数字:每人最大订购盘数和每天人数之和。我已经生成了每天的人数——这很容易。我苦苦挣扎的地方是构建一个查询,该查询可以随着日期范围的扩大而动态求和。
我能够生成查询,为我提供给定日期最大值所需的数字。我的问题是将其转换为在一次查询中跨所有可能日期生成此数字的内容。下面是一个范围从 10/1 到 10/1 的查询示例:
select sum(max_cumulative_plates_ordered) from (
select guest_id, max(cumulative_plates_ordered) as max_cumulative_plates_ordered
from raw_data
where most_recent_plate_ordered_date <= '2020-10-01'
group by 1
)
有什么想法吗?感觉这个问题好难回答。
I was able to generate the query that gives me the desired number for a given date max. My problem is translating this into something that generates this number across all possible dates in one query
不只是想要 group by
子句中的日期?
select dt,
sum(cumulative_plates_ordered) as cumulative_plates_ordered,
count(*) as number_of_people
from (
select guest_id,
most_recent_plate_ordered_date::date as dt,
max(cumulative_plates_ordered) as cumulative_plates_ordered
from raw_data
group by 1,2
) t
group by dt
编辑
如果你想把“缺失”的日期记入账户,那就有点不同了。您可以使用 cross join
生成天数和客人的所有可能组合。然后用window函数补缺:
select dt,
sum(cumulative_plates_ordered) as cumulative_plates_ordered,
count(*) as number_of_people
from (
select g.guest_id, d.dt,
max(max(t.cumulative_plates_ordered)) over(order by d.dt) as cumulative_plates_ordered
from (select distinct most_recent_plate_ordered_date::date as dt from raw_data) d
cross join (select distinct guest_id from raw_data) g
left join raw_data t
on t.guest_id = g.guest_id
and t.most_recent_plate_ordered_date >= d.dt
and t.most_recent_plate_ordered_date < d.dt + interval 1 day
group by g.guest_id, d.dt
) t
group by dt
如果我没理解错的话,你想要:
- distinct 人在特定日期订购的人数。
- 当日最大订单数
cumulative_plates_ordered
的总和
然而,这表明 2020-10-03 的值实际上是 4 而不是 5。
一种方法是关联子查询:
select dte::date,
(select count(distinct guest_id)
from t
where t.most_recent_place_ordered <= gs.dte
) as num_guests,
(select sum(plates)
from (select t.guest_id, max(t.cumulative_plates_ordered) as plates
from t
where most_recent_place_ordered <= gs.dte
group by t.guest_id
) t
) as num_plates
from (select distinct most_recent_place_ordered as dte from t) gs;
使您的数据具有挑战性的是累积总和。您可以使用 lag()
获取特定日期的更改。有了这些数据,使用 window 函数和聚合得到你想要的结果就简单多了:
with net as (
select t.*,
row_number() over (partition by guest_id order by most_recent_place_ordered) as seqnum,
cumulative_plates_ordered - coalesce(lag(cumulative_plates_ordered) over (partition by guest_id order by most_recent_place_ordered), 0) as new_plates
from t
)
select most_recent_place_ordered,
sum(sum( (seqnum = 1)::int )) over (order by most_recent_place_ordered rows between unbounded preceding and current row) as num_guests,
sum(sum( new_plates )) over (order by most_recent_place_ordered rows between unbounded preceding and current row) as num_plates
from net
group by most_recent_place_ordered
order by most_recent_place_ordered;
Here 是一个 db<>fiddle.