-
Group By에서 문자열 합치기DB/Oracle & tibero 2022. 5. 10. 23:32
Oracle
-- 버전별
WITH t AS (
SELECT '과일' type, '사과' name, '0' code FROM dual
UNION ALL SELECT '과일', '레몬', '1' FROM dual
UNION ALL SELECT '과일', '포도', '2' FROM dual
UNION ALL SELECT '과일', '참외', '3' FROM dual
UNION ALL SELECT '채소', '오이', '0' FROM dual
UNION ALL SELECT '채소', '당근', '1' FROM dual
UNION ALL SELECT '채소', '호박', '2' FROM dual
)
SELECT type
, SUBSTR(XMLAgg(XMLElement(x, ',', name) ORDER BY
code).Extract('//text()'), 2) name_9i
, wm_concat(name) name_10g
, ListAgg(name, ',') WITHIN GROUP(ORDER BY code) name_11g
FROM t
GROUP BY type
ORDER BY type
;TYPE NAME_9i NAME_10G NAME_11G 과일 사과,레몬,포도,참외 사과,레몬,포도,참외 사과,레몬,포도,참외 채소 오이,당근,호박 오이,당근,호박 오이,당근,호박 Tibero
WITH t AS (
SELECT '과일' type, '사과' name, '0' code FROM dual
UNION ALL SELECT '과일', '레몬', '1' FROM dual
UNION ALL SELECT '과일', '포도', '2' FROM dual
UNION ALL SELECT '과일', '참외', '3' FROM dual
UNION ALL SELECT '채소', '오이', '0' FROM dual
UNION ALL SELECT '채소', '당근', '1' FROM dual
UNION ALL SELECT '채소', '호박', '2' FROM dual
)
SELECT type
, Aggr_Concat(name, ',' ORDER BY code) name_Tibero
FROM t
GROUP BY type
ORDER BY type
;TYPE NAME_TIBERO 과일 사과,레몬,포도,참외 채소 오이,당근,호박 출처 : http://www.gurubee.net/article/55512
'DB > Oracle & tibero' 카테고리의 다른 글
DISTINCT와 GROUP BY의 차이 (0) 2022.05.11