Replicate data by number of months - SQL Teradata

32 views Asked by At

I need help from the community, I have a table that has the following information:

Date Number of Months Value
2019-01-01 1 7
2019-01-01 3 5
2019-02-01 2 9

Depending on the number of months, I need to replicate this for the following months and have an output like this:

Date Number of Months Value
2019-01-01 1 7
2019-01-01 3 5
2019-02-01 3 5
2019-03-01 3 5
2019-02-01 2 9
2019-03-01 2 9

How can I achieve this with SQL on Teradata?

1

There are 1 answers

0
Fred On

You can use Teradata's EXPAND ON syntax:

create volatile table mytable (TheDate date , NbrMonths smallint , _Value integer) 
no primary index on commit preserve rows;
insert mytable values(date'2019-01-01',1,7);
insert mytable values(date'2019-01-01',3,5);
insert mytable values(date'2019-02-01',2,9);

select begin(pd) as TheDate, NbrMonths, _Value
from mytable
expand on period (TheDate, (TheDate+interval '1' month * NbrMonths)) pd
by anchor month_begin;