在Oracle上将逗号分隔的值拆分为具有其他列的行[重复]

bxjv4tth  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(111)

此问题已在此处有答案

How to convert comma separated values to rows in oracle?(6个回答)
How to Split a comma separated string in Oracle [duplicate](3个答案)
上个月关门了。
我有这个数据:

需要转换成这样:

我看到了一些关于regexp_substr的示例,但我似乎不能让它在我的过程中工作。

With folder_query as 
   (
       SELECT * FROM
       (SELECT ID, CardNo, REPLACE(LICENCE (RSN, 1), '<br/>', '') AS TagNo
         FROM FOLDER1) SQ
   )
   SELECT ID, CardNo,regexp_substr(TagNo,'[^,]+',1,level) AS TagNo
   FROM folder_query
   CONNECT by level <= REGEXP_COUNT(TagNo, '[^,]+')

我犯了一些错误:
ORA-30563:此处不允许使用外部连接运算符(+)30563。00000 -“此处不允许使用外部连接运算符(+)”* 原因:试图在select-list、SECTECT BY子句、START WITH子句或ORDER BY子句中引用(+)。* 操作:不要在select-list、PRESECT BY子句、START WITH子句或ORDER BY子句中使用运算符。

uqzxnwby

uqzxnwby1#

这里有一个选项,用于样本数据:

SQL> select * from test;

        ID     CARDNO TAGNO
---------- ---------- -----------------------
     84773  317012878 AA7 101 443,AA5 101 453    
     84774  317012878 AA2 101 493

查询方式:

SQL> select id, cardno,
  2    regexp_substr(tagno, '[^,]+', 1, column_value) tagno
  3  from test cross join
  4    table(cast(multiset(select level from dual
  5                        connect by level <= regexp_count(tagno, ',') + 1
  6                       ) as sys.odcinumberlist));

        ID     CARDNO TAGNO
---------- ---------- --------------------
     84773  317012878 AA7 101 443
     84773  317012878 AA5 101 453
     84774  317012878 AA2 101 493

SQL>
y4ekin9u

y4ekin9u2#

with rws as (
  select '84773' id, 'AA7 101 443,AA5 101 453'  tagno from dual UNION ALL 
  select '84774' id, 'AA2 101 493' tagno from dual 
),
rcte (id, tagno, cnt, lvl, value) as (
  select id, tagno, length ( tagno ) - length ( replace ( tagno, ',' ) ), 1,
       regexp_substr (
           tagno,
           '[^,]+',
           1,
           1
         )
  from   rws
  union all
  select id, tagno, cnt, lvl + 1,
       regexp_substr (
           tagno,
           '[^,]+',
           1,
           lvl + 1
         )
  from   rcte
  where  lvl <= cnt
)
select id, value
from rcte
order by id, value;

ID    VALUE
84773 AA5 101 453
84773 AA7 101 443
84774 AA2 101 493

相关问题