Currently in the old system we have, the code generates a sequence of dates, and then that sequence is joined against the inner query it's created from. What this ends up with (the eventual result), is a table that returns information about a specific date as if it were a date in the past, e.g. we had a value of X for 2/2/2022 on 1/2/2022, and then we had a value of X+3 for 2/2/2022 on 1/14/2022. So overtime we can see that whatever X is is growing.
We are currently migrating from a legacy system to Snowflake. I am trying to re-write this in Snowflake. The issue I am having is that the sequence function called in the old system is not one that works in Snowflake, and I need to figure out an equivalent method. I have tried reading about Snowflake sequences, but I don't quite understand how to write it in a way that achieves this same result.
Any help appreciated!
Here is a snippet of the old code followed by code I've tried:
OLD CODE:
--this thing is inside an inner query called inner_query
sequence(
date_add('day', -182, a_beginning_date_formated_as_a_date),
date_add('day', 40, an_end_date_formated_as_a_date)
) as ranges
--this cross join joins that inner query to the ranges inside the inner query
cross join unnest(ranges) as t (range)
-
-
-
NEW CODE:
--I tried to get lucky by copying the old code verbatem
create sequence ranges(
date_add('day', -182, a_beginning_date_formated_as_a_date),
date_add('day', 40, an_end_date_formated_as_a_date))
--I tried this code that I found on StackOverflow
(select -1 + row_number() over(order by 0) i, start_date + i generated_date
from (select dateadd(day,-182,start_date)::date start_date, dateadd(day,40,end_date)::date end_date)
join table(generator(rowcount => 10000 )) x qualify i < 1 + end_date - start_date) as ranges,