0

我是 Oracle 技术的新手。早些时候,由于缺乏对要求的理解,我针对同一问题发布了 2 个帖子。

表格1:

MSGID
-----
1,2,3
2,3
4
null
null

表 2:

MID   MSGDESC
----  -------
1     ONE
2     TWO
3     THREE
4     FOUR

预期输出:

XCOL     DESC
-----    -----
1,2,3    ONE,TWO,THREE
2,3      TWO,THREE
4        FOUR

我无法满足这个要求。请给我一个解决方案。

注意:表没有任何唯一或主键值。表 1 有 5000 条记录,表 2 只有 80 条带描述的记录。

4

1 回答 1

0
create table Table1 (MSGID varchar2(100));

insert into Table1 values ('1,2,3');
insert into Table1 values ('2,3');
insert into Table1 values ('4');
insert into Table1 values (null);
insert into Table1 values (null);

create table Table2 (MID varchar2(100), MSGDESC varchar2(100));

insert into Table2 values ('1','ONE');
insert into Table2 values ('2','TWO');
insert into Table2 values ('3','THREE');
insert into Table2 values ('4','FOUR');

select
  msgid as xcol, 
  "DESC", 
  col1, col2, ..., col12
from
  Table1
  left join (
    select
       msgid,
       wm_concat(msgdesc) as "DESC"
    from
       (
          select
             msgid,
             msgdesc
          from
             (select distinct msgid from Table1 where ...)
             cross join (
                select level as occ from dual connect by level <= 100)
             )
             left join Table2
                on mid = regexp_substr(msgid, '[^,]+', 1, occ)
          where
             occ <= regexp_count(msgid, ',') + 1
          order by msgid, occ
       )
    group by msgid
  ) using (msgid)
于 2013-04-16T18:02:17.690 回答