Last active
September 11, 2023 14:33
-
-
Save jonathanlxy/9d93d0084e9e7ca8c9e59feff33e499f to your computer and use it in GitHub Desktop.
Generate continuous dates between two dates in snowflake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Reference: https://community.periscopedata.com/t/18wkh8/generate-series-of-dates-in-snowflake | |
-- Documentation of SEQx(): https://docs.snowflake.net/manuals/sql-reference/functions/seq1.html | |
-- Since SEQx() does not guarantee gap-free sequence, row_number() needs to be used on top of SEQx() | |
SELECT activity_date, calendar, COUNT(1) | |
FROM table1 AS t1 | |
RIGHT JOIN (SELECT row_number() OVER (ORDER BY SEQ4()) AS days | |
, TO_DATE(DATEADD(day, days, '2019-05-01')) AS calendar | |
FROM TABLE(GENERATOR(rowcount=>80))) | |
ON t1.activity_date = calendar | |
GROUP BY 1, 2 | |
ORDER BY 2, 1 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment