我在这张表中包含以下数据
Job Quantity Status Repeat
1 100 OK 2
2 400 HOLD 0
3 200 HOLD 1
4 450 OK 3
根据每行的Repeat列中的值,应该再次重复该行。例如,对于作业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
答案 0 :(得分:8)
您可以使用递归CTE:
with cte(Job, Repeat, i) as
(
select Job
, Repeat
, 0
from YourTable
union all
select Job
, Repeat
, i + 1
from cte
where cte.i < cte.Repeat
)
select *
from cte
order by
Job
, i
答案 1 :(得分:7)
假设每行不会生成超过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;
答案 2 :(得分:-1)
不是使用查询进行此操作,而是可以先在数据表中获取此数据,然后根据repeat的值将行添加到新数据表中,然后绑定此新数据表。