小编典典

根据每行中的列值重复行

sql

我有这张表,其中包含以下数据

Job  Quantity   Status  Repeat 
1    100         OK     2 
2    400         HOLD   0 
3    200         HOLD   1 
4    450         OK     3 

根据每行“重复”列中的值,应再次重复该行。例如,对于“作业1”,“重复”值为2,因此“作业1”应重复两次以上。

结果表应如下

Job      Quantity   Status  Repeat 
1        100        OK      2 
1        100        OK      2 
1        100        OK      2 
2        400        HOLD    0 
3        200        HOLD    1 
3        200        HOLD    1 
4        450        OK      3 
4        450        OK      3 
4        450        OK      3 
4        450        OK      3 

有人可以帮我解决这个问题吗?我正在使用oracle 10g


阅读 199

收藏
2021-03-17

共1个答案

小编典典

假设您每行不会产生超过1000行:

with num as (select level as rnk from dual connect by level<=1000)
select Job,  Quantity,   Status,  Repeat, rnk 
from t join num on ( num.rnk <= repeat )
order by job, rnk;

这是一个测试:http :
//sqlfiddle.com/#!4/4519f/12

更新: 正如Jeffrey Kemp所说,您可以通过子查询“检测”最大值:

with num as (select level as rnk 
             from dual 
             connect by level<=(select max(repeat) from t)
             )
select job,  quantity,   status,  repeat, rnk 
from t join num on ( num.rnk <= repeat )
order by job, rnk;
2021-03-17