Oracle duplicate row N times where N is a column

拈花ヽ惹草 提交于 2019-12-05 07:02:08

You can do it with a hierarchical query:

SQL Fiddle

Query 1:

WITH levels AS (
  SELECT LEVEL AS lvl
  FROM   DUAL
  CONNECT BY LEVEL <= ( SELECT MAX( DupCount ) FROM TestTable )
)
SELECT Name,
       DupCount
FROM   TestTable
       INNER JOIN
       levels
       ON ( lvl <= DupCount )
ORDER BY Name

Results:

|  NAME | DUPCOUNT |
|-------|----------|
|  Jane |        1 |
|  Jeff |        3 |
|  Jeff |        3 |
|  Jeff |        3 |
|  Mark |        2 |
|  Mark |        2 |
| Steve |        1 |

You can do this with a recursive cte. It would look like this

with cte as (name, dupcount, temp)
(
   select name,
          dupcount,
          dupcount as temp
   from testtable
     union all
   select name, 
          dupcount,
          temp-1 as temp
   from cte 
   where temp > 1
)
select name, 
       dupcount
from cte
order by name
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!