![](/img/trans.png)
[英]How to expand records based on start and end date in SQL or PL/SQL
[英]Expand start and end date in sql Snowflake
我有以下問題:
在雪花數據庫表中,我有一個名為開始日期的字段和另一個名為結束日期的字段。
我需要的是擴展每條記錄的日期,例如:
id | date_start | date_end
1 | 2019-12-01 | 2019-12-05
2 | 2020-01-01 | 2020-01-06
結果應如下所示:
id | date_new
1 | 2019-12-01
1 | 2019-12-02
1 | 2019-12-03
1 | 2019-12-04
1 | 2019-12-05
2 | 2020-01-01
2 | 2020-01-02
2 | 2020-01-03
2 | 2020-01-04
2 | 2020-01-05
謝謝
一種選擇是遞歸查詢:
with recursive cte (id, date_start, date_end) as (
select id, date_start, date_end from mytable
union all
select id, date_start + interval '1 day', date_end from cte where date_start < date_end
)
select id, date_start date_new from cte
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.