如何根据 Teradata 中的列值复制 table 行?

How to duplicate table row based on column value in Teradata?

有人可以帮忙完成这个任务吗?如果我在 Teradata 中有一个 table,如下所示:

案例编号 内容 重复
id1 第 1 行 2
id2 行 2 3

我想根据下面的重复值构建一个新的 table。我该怎么办?

案例编号 内容 重复 groupid
id1 第 1 行 2 1
id1 第 1 行 2 2
id2 行 2 3 1
id2 行 2 3 2
id2 行 2 3 3

谢谢!

您可以使用递归 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

Teradata 专有的 EXPAND ON 语法创建时间序列并可用于此任务:

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