Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

generate_series equivalent in snowflake

I’m used to Postgres and when calculating MRR and ARR I would use some version of this:

date_trunc('day',transaction_date) + interval '1' month * generate_series(0,11)

I’ve been having trouble finding analogous syntax in Snowflake. Most of what I’m seeing something akin to a date_dimensions table but I need to be able to create virtual billing dates each month and apply some fraction of transaction to each date.

For example, if there’s a $1,000 annual subscription purchase made on 3/29, then I would create billing dates for the next 12 months (4/29,5/29,6/29 etc.) with a corresponding 1000/12 value.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve seen documentation on generator and seq4() but having a hard time applying it to what I need.

Thank you.

>Solution :

Using GENERATOR and DATEADD:

SELECT DATEADD(MONTH, (ROW_NUMBER() OVER(ORDER BY seq8())-1),
               DATE_TRUNC('day',transaction_date))
FROM tab
,TABLE(GENERATOR(ROWCOUNT => 12));

Output:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading