2010-01-20 92 views
2

尋找在ID相同時生成逗號分隔的語言列表。SSIS:Oracle多行到一列未輸出STRAGG輸出

表1:

ID | LangID 
1 1 
1 2 
1 3 
2 3 
2 4 
3 1 

表2:

ID | Language 
1 English 
2 Spanish 
3 French 
4 German 

要產生的結果是這樣的:

ID | Languages 
1 English, Spanish, French 
2 French, German 
3 English 

事情我已經嘗試:

Stragg - SSIS給我「無效標識符「

SYS_Connect_By_Path - 無法弄清楚如何將ID轉換爲字符串語言。

有什麼建議嗎?

回答

6

執行字符串聚合以產生逗號分隔列表有多種方法 - 請參閱this link for more details。基於鏈接的例子,嘗試:

SELECT x.id, 
     LTRIM(MAX(SYS_CONNECT_BY_PATH(x.language,',')) 
     KEEP (DENSE_RANK LAST ORDER BY curr),',') AS employees 
    FROM (SELECT a.id, 
       b.language, 
       ROW_NUMBER() OVER (PARTITION BY a.id ORDER BY b.language) AS curr, 
       ROW_NUMBER() OVER (PARTITION BY a.id ORDER BY b.language) -1 AS prev 
      FROM TABLE_1 a 
      JOIN TABLE_2 b ON b.id = a.langid) x 
GROUP BY x.id 
CONNECT BY prev = PRIOR curr AND x.id = PRIOR x.id 
START WITH curr = 1; 
+0

也做到了,謝謝主席先生。 – 2010-01-20 20:30:17

2

爲11g中,檢查出listagg function

select t1.id, listagg(t2.language, ', ') within group (order by t2.language) 
from t1, t2 
where t1.langId = t2.id 
group by t1.id;