select语句中的重复行

时间:2015-03-03 15:51:50

标签: sql oracle

例如,我有两行

id|amount
 1|4
 2|1

如何复制超过1的行?

1|1
1|1
1|1
1|1
2|1

1 个答案:

答案 0 :(得分:3)

使用connect by:

可以轻松实现
with sample_data as (select 1 id, 4 amount from dual union all
                     select 2 id, 1 amount from dual)
select id, 1
from   sample_data
connect by prior id = id
           and prior dbms_random.value is not null
           and level <= amount;

        ID          1
---------- ----------
         1          1
         1          1
         1          1
         1          1
         2          1