0

Can someone help this task? If I have one table as below in Teradata:

caseid content repeat
id1 row1 2
id2 row2 3

I want to build a new table based on the repeat value as below. How should I do?

caseid content repeat groupid
id1 row1 2 1
id1 row1 2 2
id2 row2 3 1
id2 row2 3 2
id2 row2 3 3

Thanks!

coco
  • 871
  • 1
  • 7
  • 15

2 Answers2

3

Teradata proprietary EXPAND ON syntax creates time series and can be used for this task:

SELECT t.*
   -- convert period back to int
  ,End(pd) - Current_Date AS groupid
FROM mytable AS t
-- works on date/time only -> convert int to period
EXPAND ON PERIOD(Current_Date, Current_Date + repeat) AS pd 
dnoeth
  • 59,503
  • 4
  • 39
  • 56
1

you can do it using recursive cte :

with recursive cte as (
select * , 1 groupid from cases 
union all 
select caseid ,content, repeat, groupid + 1 groupid
from cte 
where groupid  < repeat
)

select  * from cte
order by caseid
caseid | content | repeat | groupid
-----: | :------ | -----: | ------:
     1 | row1    |      2 |       1
     1 | row1    |      2 |       2
     2 | row2    |      3 |       1
     2 | row2    |      3 |       2
     2 | row2    |      3 |       3

db<>fiddle here

eshirvana
  • 23,227
  • 3
  • 22
  • 38