0

假设我有一个数据集

----------------------
 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 次数。

4

3 回答 3

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

小提琴:http ://sqlfiddle.com/#!4/01f5b/12

于 2013-11-13T04:38:16.017 回答
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;
于 2013-11-13T04:33:36.993 回答
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;
于 2013-11-13T05:11:24.480 回答