1

I have a table as below

id           date.         no_of Days
12           12-03-1999    30
13           12-03-1999    7
14           12-03-1999    22
15           12-03-1999    12

I need to create another table with one extra field as date_increment which will be based on given date incremented to 1 day based no_of_days. Output will be like given below

id           date.         no_of Days.  date_increment
12           12-03-1999    30           12-03-1999
12           12-03-1999    30           13-03-1999
12           12-03-1999    30           14-03-1999
..           .....         ..           till 30 days
13           12-03-1999    7            12-03-1999
13           12-03-1999    7            13-03-1999
13           12-03-1999    7            14-03-1999
..           ...           ..           till 7 day
14           12-03-1999    22
15           12-03-1999    12

I am writing this query for snowflake which does not support loop or dynamic query. can somebody please suggest.

Even If i use another IDE like python, I am not sure how to address this.

danD
  • 666
  • 1
  • 7
  • 29

1 Answers1

4

One method is to use generator():

select t.*, date + (n - 1) * interval '1 day'
from t join
     (select seq4() as n
      from table(generator(rowcount => 100)) g
     ) n
     on n.n <= t.no_of_days;

Hans Henrik Eriksen
  • 2,690
  • 5
  • 12
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • thanks a lot my friend.. only thing i had to change was instead of numrows.. i had to take ROWCOUNT – danD Dec 10 '19 at 18:12