Oracle遍历列上的值(Oracle 11)

时间:2019-05-14 11:54:57

标签: oracle oracle11g

所以我有一些像这样的数据

NO| ID    | PID | COUNT
1 | 00033 | P4  | 1
2 | 00033 | P3  | 3
3 | 00033 | P2  | 2

我想像这样基于计数值迭代ID和PID

NO| ID    | PID
1 | 00033 | P4
2 | 00033 | P3
3 | 00033 | P3
4 | 00033 | P3
5 | 00033 | P2
6 | 00033 | P2

实际上已经使用此查询解决了

SELECT row_number() OVER ( ORDER BY t."ID", t."PID" DESC ) as NO, 
       t."ID", t."PID"
FROM Table1 t
CROSS APPLY(
  SELECT 1 FROM dual
  CONNECT BY level <= t."COUNT"
)
ORDER BY t."ID", t."PID" DESC

按照此链接 Oracle iterates thru values on columns

但是由于我们的数据库使用的是oracle 11,因此代码不再起作用。

我想知道与oracle 11相同的方法。

Rian,

1 个答案:

答案 0 :(得分:4)

将此分层查询作为数字表进行广播,然后加入:

with 
  t("NO", "ID", "PID", "COUNT") as (
    select 1, '00033', 'P4', 1 from dual union all
    select 2, '00033', 'P3', 3 from dual union all
    select 3, '00033', 'P2', 2 from dual ),
  a as (select t.*, 
               cast(multiset(select level lvl 
                             from dual 
                             connect by level <= "COUNT") 
                    as sys.odcinumberlist) nums 
          from t)
select "NO", "ID", "PID" from a cross join table(nums) 

结果:

        NO ID    PID
---------- ----- ---
         1 00033 P4
         2 00033 P3
         2 00033 P3
         2 00033 P3
         3 00033 P2
         3 00033 P2
6 rows selected