简体   繁体   English

给定两个日期列,如何将数据转换为每日快照?

[英]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 1个 12 12 2022-01-01 2022-01-01 2022-02-01 2022-02-01 15 15
2 2个 234 234 2022-11-01 2022-11-01 2022-12-31 2022-12-31 123 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 1个 12 12 15 15 2022-01-01 2022-01-01
1 1个 12 12 15 15 2022-01-02 2022-01-02
1 1个 12 12 15 15 2022-01-03 2022-01-03
... ... ... ... ... ... ... ...
2 2个 234 234 123 123 2022-12-31 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 1个 12 12 15 15 2022-01-01 2022-01-01
1 1个 12 12 15 15 2022-01-02 2022-01-02
1 1个 12 12 15 15 2022-01-03 2022-01-03
1 1个 12 12 15 15 2022-01-04 2022-01-04
1 1个 12 12 15 15 2022-01-05 2022-01-05
2 2个 234 234 123 123 2022-11-01 2022-11-01
2 2个 234 234 123 123 2022-11-02 2022-11-02
2 2个 234 234 123 123 2022-11-03 2022-11-03

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

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