2013-04-16 48 views
0

我是Oracle技術新手。之前我因爲缺乏對這個要求的理解而發佈了同一期的2篇文章。如何在Oracle 10g中基於逗號分隔值顯示逗號分隔的描述?

表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條記錄和說明。

+1

你嘗試過什麼?這看起來像是一個兩步過程;瞭解如何將表1中的CSV ID作爲單獨的行對待(在此搜索以獲取方式),然後將結果加入到表2中。 –

+0

嗨,根據您的建議,我在發佈此項之前完成了相同的任務但我無法得到這個。你可以評論它嗎?或者你可以發佈代碼,我如何得到這個結果。這是非常迫切的要求。請給我提供一步步的過程 – Prasad

回答

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) 
+0

嗨葉戈,謝謝你的回覆,wm_concat在oracle 10g中有效嗎? – Prasad

+0

@Prasad - 有效。 –

+0

非常感謝您的答覆。我必須在我的select語句中使用12個必填列,應使用上述查詢中的那些列名稱,如從table1和table2中選擇xcol,col1,col2 ... desc。在我的實際需求中,我必須在8個表連接中使用上述編碼。 – Prasad