[英]How to transform data into daily snapshot given the two date columns?
I have product data in my table which looks similar to this我的表中有产品数据,看起来与此类似
product_id![]() |
user_id![]() |
sales_start![]() |
sales_end![]() |
quantity![]() |
---|---|---|---|---|
1 ![]() |
12 ![]() |
2022-01-01 ![]() |
2022-02-01 ![]() |
15 ![]() |
2 ![]() |
234 ![]() |
2022-11-01 ![]() |
2022-12-31 ![]() |
123 ![]() |
I want to transform the table into a daily snapshot so that it would look something like this:我想将表格转换为每日快照,使其看起来像这样:
product_id![]() |
user_id![]() |
quantity![]() |
date![]() |
---|---|---|---|
1 ![]() |
12 ![]() |
15 ![]() |
2022-01-01 ![]() |
1 ![]() |
12 ![]() |
15 ![]() |
2022-01-02 ![]() |
1 ![]() |
12 ![]() |
15 ![]() |
2022-01-03 ![]() |
... ![]() |
... ![]() |
... ![]() |
... ![]() |
2 ![]() |
234 ![]() |
123 ![]() |
2022-12-31 ![]() |
I know how to do a similar thing in Pandas, but I need to do it within AWS Athena.我知道如何在 Pandas 中做类似的事情,但我需要在 AWS Athena 中进行。 I thought of getting the date interval and unnest it, but I am struggling with mapping them properly.
我想获取日期间隔并将其取消嵌套,但我正在努力正确映射它们。
Any ideas on how to transform data?关于如何转换数据的任何想法?
This will help you sequence
这将帮助您
sequence
SELECT product_id, user_id, quantity, date(date) as date FROM(
VALUES
(1, 12, DATE '2022-01-01', DATE '2022-02-01', 15),
(2, 234, DATE '2022-11-01', DATE '2022-12-31', 123)
) AS t (product_id, user_id, sales_start, sales_end, quantity),
UNNEST(sequence(sales_start, sales_end, interval '1' day)) t(date)
You can usesequnece
to generate dates range and then unnest
it:您可以使用
sequnece
生成日期范围,然后unnest
嵌套:
-- sample data
with dataset(product_id, user_id, sales_start, sales_end, quantity) as (
values (1, 12 , date '2022-01-01', date '2022-01-05', 15), -- short date ranges
(2, 234, date '2022-11-01', date '2022-11-03', 123) -- short date ranges
)
-- query
select product_id, user_id, quantity, date
from dataset,
unnest(sequence(sales_start, sales_end, interval '1' day)) as t(date);
Output: Output:
product_id![]() |
user_id![]() |
quantity![]() |
date![]() |
---|---|---|---|
1 ![]() |
12 ![]() |
15 ![]() |
2022-01-01 ![]() |
1 ![]() |
12 ![]() |
15 ![]() |
2022-01-02 ![]() |
1 ![]() |
12 ![]() |
15 ![]() |
2022-01-03 ![]() |
1 ![]() |
12 ![]() |
15 ![]() |
2022-01-04 ![]() |
1 ![]() |
12 ![]() |
15 ![]() |
2022-01-05 ![]() |
2 ![]() |
234 ![]() |
123 ![]() |
2022-11-01 ![]() |
2 ![]() |
234 ![]() |
123 ![]() |
2022-11-02 ![]() |
2 ![]() |
234 ![]() |
123 ![]() |
2022-11-03 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.