假设我有一个数据集
----------------------
col1 | col2 | col3 |
----------------------
a b 3
c d 2
----------------------
现在SQL中有一种方法可以选择
----------------------
colx | coly | colz |
----------------------
a b 1
a b 2
a b 3
c d 1
c d 2
----------------------
即。 col1和col2重复col3次数。
答案 0 :(得分:1)
SELECT DISTINCT t.col1 AS colx, t.col2 AS coly, level AS colz
FROM tablee t
CONNECT BY level <= t.col3
ORDER BY t.col1, t.col2, level
答案 1 :(得分:0)
请尝试:
with T (colx , coly , colz , mxcol3) as
(
select col1, col2, 1 col3, max(col3) over (partition by col1, col2) mxcol3
from YourTab
union all
select colx , coly , colz +1 colz , mxcol3
from T
where colz +1<=mxcol3
)
select
colx, coly, colz
From T
order by colx, coly, colz;
答案 2 :(得分:0)
如果您使用的是11gR2,则可以使用递归CTE。
with cte(x,y,z) as (
select col1, col2, col3 --select all the records from the table
from table_name
union all
select x, y, z-1 --select previously selected rows and decrement the col3 by 1
from cte --until the col3 becomes 1
where z > 1
)
select *
from cte
order by x,y,z;