Repeat each value n times as rows in SQL

前端 未结 4 1612
盖世英雄少女心
盖世英雄少女心 2021-01-28 05:36

I have been trying to achieve this in SQL (Oracle 11g) for a while but could not find a proper way to do it.

My table names has the following rows:

相关标签:
4条回答
  • 2021-01-28 05:54

    Try this:

    select * from names
    cross join (select rownum n from dual
                connect by level <= (select max(repeat) from names))
    where n <= repeat
    order by name
    
    0 讨论(0)
  • 2021-01-28 05:56

    You may use some temp table containing list of numbers 1 to N, where N is the highest number in your table names. Let call it num(o int) Then the query will be

    SELECT *
    FROM names, num
    WHERE num.o <= names.repeat
    
    0 讨论(0)
  • 2021-01-28 05:59

    If we presume that your all_objects system object has more objects than the max repeat...

    SELECT n.name
    FROM names n
    LEFT JOIN (Select rownum from all_objects) z
     on z.rowNum < n.repeat
    
    0 讨论(0)
  • 2021-01-28 06:10

    I just found an alternative with connect by and correlated sub-query.

    select name
    from   names o
    connect by level <= ( select repeat from names 
                            i where i.name = o.name )
    AND                        
    prior name = name and
               prior sys_guid() is not null
               order by name;
    
    0 讨论(0)
提交回复
热议问题