我在这张表中有以下数据
@H_301_2@Job Quantity Status Repeat
1 100 OK 2
2 400 HOLD 0
3 200 HOLD 1
4 450 OK 3
根据每行的“重复”列中的值,应再次重复该行.例如,对于作业1,重复值为2,因此作业1应重复两次.
结果表应如下所示
@H_301_2@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
解决方法
假设每行不会生成超过1000行:
@H_301_2@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所说,你可以用子查询“检测”最大值:
@H_301_2@with num as (select level as rnk from dual connect by level<=(select max(repeat) from t) ) select job,quantity,status,repeat,rnk;