Snowflake - 获取记录的开始日期和结束日期之间的日期列表

问题描述 投票:0回答:1

我需要在开始日期和结束日期之间的每一天重复记录 字段是:

ID
,
Name
,
Start_date
,
End_date

值是:

001, Record1, 2023-04-01 00:00:00.000, 2023-06-30 00:00:00.000

我试图得到这个:

001, Record1, [Every single day between these dates]

- 001, Record1, 2023-04-01 
- 001, Record1, 2023-04-02 
- 001, Record1, 2023-04-03
- --- so on and so on 
- 001, Record1, 2023-06-29 
- 001, Record1, 2023-06-30
sql snowflake-cloud-data-platform recursive-query date-arithmetic
1个回答
0
投票

这是 递归 CTE 的典型用例。在雪花中:

with recursive rcte as (
    select id, name, start_date, end_date from mytable
    union all
    select id, name, start_date + interval '1 day', end_date from rcte where start_date < end_date
)
select * from id, name, start_date from rcte
© www.soinside.com 2019 - 2024. All rights reserved.