英文:
Snowflake Generate row for every date between 2 date columns
问题
I'm working in Snowflake trying to explode rows in a table where each row is currently based on a date range provided by start_date
and end_date
columns. I need to turn this information into a single row for every date in that date range.
Below is my current table as of right now. In this example there's a row with a date range that spans 5 days with a price of $5, so I want to turn this 1 row into 5 rows for every date in that date range while retaining that same pricing information of $5 for every row.
start_date | end_date | price |
---|---|---|
2023-05-22 | 2023-05-26 | 5 |
Proposed Solution 1
start_date | end_date | price |
---|---|---|
2023-05-22 | 2023-05-22 | 5 |
2023-05-23 | 2023-05-23 | 5 |
2023-05-24 | 2023-05-24 | 5 |
2023-05-25 | 2023-05-25 | 5 |
2023-05-26 | 2023-05-26 | 5 |
Proposed Solution 2
date | price | |
---|---|---|
2023-05-22 | 5 | |
2023-05-23 | 5 | |
2023-05-24 | 5 | |
2023-05-25 | 5 | |
2023-05-26 | 5 |
Either solution will work for my use case. In Postgresql i've used generate_series
and had good results, but while researching this for Snowflake I'm not really seeing any great solutions so far. Any help would be appreciated!
英文:
I'm working in Snowflake trying to explode rows in a table where each row is currently based on a date range provided by start_date
and end_date
columns. I need to turn this information into a single row for every date in that date range.
Below is my current table as of right now. In this example there's a row with a date range that spans 5 days with a price of $5, so I want to turn this 1 row into 5 rows for every date in that date range while retaining that same pricing information of $5 for every row.
start_date | end_date | price |
---|---|---|
2023-05-22 | 2023-05-26 | 5 |
Proposed Solution 1
start_date | end_date | price |
---|---|---|
2023-05-22 | 2023-05-22 | 5 |
2023-05-23 | 2023-05-23 | 5 |
2023-05-24 | 2023-05-24 | 5 |
2023-05-25 | 2023-05-25 | 5 |
2023-05-26 | 2023-05-26 | 5 |
Proposed Solution 2
date | price | |
---|---|---|
2023-05-22 | 5 | |
2023-05-23 | 5 | |
2023-05-24 | 5 | |
2023-05-25 | 5 | |
2023-05-26 | 5 |
Either solution will work for my use case. In Postgresql i've used generate_series
and had good results, but while researching this for Snowflake I'm not really seeing any great solutions so far. Any help would be appreciated !
答案1
得分: 2
感谢建议!Lukasz的解决方案非常完美。非常感谢!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论