2016-11-22 15 views
0

我正在查找可以從多個表中取多個字段並將它們合併到一個字段中的查詢。防爆。我有一個查詢,看起來像:Oracle - 從多個表到多個表的多行

select to_char(a.comments), to_char(b.comments), to_char(c.comments) 
from  crm.custprob a 
left join crm.custcall b on b.servno = a.servno 
left join crm.custlog c on c.servno = b.servno 
where a.servno = 1716942 

,併產生輸出:

 
a.comments b.comments c.comments 
1 Regular 3 Primary  5 Log 
1 Regular 3 Primary  4 Log 
1 Regular 2 Other  5 Log 
1 Regular 2 Other  4 Log 

我想是會產生所有在一行輸出的查詢,並在一個領域(我不關心數字的順序),所以它看起來像:

 
Comments 
1 Regular 2 Other 3 Primary 4 Log 5 Log 
+0

使用'''opoerator(在你的select中替換逗號與||)或嵌套'concat',因爲函數只支持2個字符串,所以你必須嵌套一個concat。 – xQbert

+0

儘管替換逗號仍然返回4行,但如果可能的話,我想將它降到1行 –

+0

ahh那麼根據您使用的oracle版本,您需要'wm_concat()'或'ListAgg()'。 – xQbert

回答

1

首先得到所有評論的列表。你會爲此使用UNION,而不是加入。然後使用LISTAGG彙總行並連接所有的評論:

select listagg(comments, ' ') within group (order by comments) as comments 
from 
(
    select to_char(comments) as comments from crm.custprob where servno = 1716942 
    union 
    select to_char(comments) as comments from crm.custcall where servno = 1716942 
    union 
    select to_char(comments) as comments from crm.custlog where servno = 1716942 
); 

(而如果不可能有重複的,你會使用UNION ALL代替UNION)。 (根據下面的評論它表明commentsNCLOB類型的更新)

+0

根據Oracle的版本,LISTAGG(11g)可能不可用。如果不支持LISTAGG,請考慮使用未記錄的功能WM_CONCAT()。 – xQbert

+0

這看起來很接近,但它引發了錯誤'ORA000904:'COMMENTS「:無效標識符」,指的是「組內(按評論排序)」中的評論,我嘗試用'crm.custprob.comments'替換它。仍然出錯 –

+0

奇怪,也許使用相同的名稱作爲別名會導致問題,你可以嘗試用'作爲comment_list'替換'as comments'嗎? –

0

下面是確實在各個表的LISTAGG前加入一種替代方案:

WITH t1 AS (SELECT 1 ID, '1 regular' comments FROM dual UNION ALL 
      SELECT 2 ID, '1 abnormal' comments FROM dual), 
    t2 AS (SELECT 1 ID, '2 other' comments FROM dual UNION ALL 
      SELECT 1 ID, '3 primary' comments FROM dual UNION ALL 
      SELECT 2 ID, '2 something else' comments FROM dual UNION ALL 
      SELECT 2 ID, '3 secondary' comments FROM dual), 
    t3 AS (SELECT 1 ID, '4 log' comments FROM dual UNION ALL 
      SELECT 1 ID, '5 log' comments FROM dual UNION ALL 
      SELECT 2 ID, '4 log' comments FROM dual UNION ALL 
      SELECT 2 ID, '5 log' comments FROM dual) 
SELECT t1.id, 
     t1.comments||' '||t_2.comments||' '||t_3.comments comments 
FROM t1 
     LEFT OUTER JOIN (SELECT ID, listagg(comments, ' ') WITHIN GROUP (ORDER BY comments) comments 
         FROM t2 
         GROUP BY ID) t_2 ON t1.id = t_2.id 
     LEFT OUTER JOIN (SELECT ID, listagg(comments, ' ') WITHIN GROUP (ORDER BY comments) comments 
         FROM t3 
         GROUP BY ID) t_3 ON t_2.id = t_3.id; 

     ID COMMENTS 
---------- -------------------------------------------------------------------------------- 
     1 1 regular 2 other 3 primary 4 log 5 log 
     2 1 abnormal 2 something else 3 secondary 4 log 5 log 
0

工作的呢?

select listagg(comments, ' ') within group (order by ordering) as comments 
from (select comments from crm.custprob, 1 as ordering where servno = 1716942 
     union all 
     select comments from crm.custcall, 2 where servno = 1716942 
     union all 
     select comments from crm.custlog, 3 where servno = 1716942 
    ) x;