SSIS: Oracle Multiple rows to one column output without STRAGG

后端 未结 2 590
悲哀的现实
悲哀的现实 2021-01-22 04:24

Looking to generate a comma separated list of languages when the ID is the same.

Table 1:

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


        
相关标签:
2条回答
  • 2021-01-22 05:08

    For 11g, check out the 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;
    
    0 讨论(0)
  • 2021-01-22 05:18

    There are various ways to perform string aggregation to produce a comma separated list - see this link for more details. Based on the example in the link, try:

    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 讨论(0)
提交回复
热议问题