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_AGG |
과일 | 사과,레몬,포도,참외 |
채소 | 오이,당근,호박 |
WITH t AS ( SELECT '과일' AS type, '사과' AS name, '0' AS code FROM dual -- 이하 마농님 코드와 동일합니다.
Group By 에서 문자열 합치기 - DBMS 별 정리 -- MySQL | MariaDB | CUBRID -- SELECT type , Group_Concat(name ORDER BY code) name_MySQL FROM t GROUP BY type ORDER BY type ;
-- MSSQL -- SELECT type , STUFF((SELECT ',' + name FROM t WHERE type = a.type ORDER BY code FOR XML PATH('') ), 1, 1, '') name_MSSQL_old , String_Agg(name, ',') WITHIN GROUP(ORDER BY code) name_MSSQL_2017 FROM t a GROUP BY type ORDER BY type ;
-- Tibero -- SELECT type , Aggr_Concat(name, ',' ORDER BY code) name_Tibero FROM t GROUP BY type ORDER BY type ;
-- Sybase -- SELECT type , List(name, ',' ORDER BY code) name_Sybase FROM t GROUP BY type ORDER BY type ;
-- PostgreSQL -- SELECT type , Array_to_String(Array_Agg(name ORDER BY code), ',') name_PostgreSQL FROM t GROUP BY type ORDER BY type ;
좋은정보 감사합니다.~
출처남기고 스크랩해갈게요~^^
-- 오류 : ORA-01489: 문자열 연결의 결과가 너무 깁니다. -- 원인 : 4000 Byte 이상 문자 연결. -- 해결 : XMLAgg 의 getCLOBval() 기능 이용. -- 해결 : 12C 의 ON OVERFLOW TRUNCATE 기능 이용. SELECT type , SUBSTR( XMLAgg(XMLElement(x, ',', name) ORDER BY code).Extract('//text()').getCLOBval() , 2) name_clob , ListAgg(name, ',' ON OVERFLOW TRUNCATE) WITHIN GROUP(ORDER BY code) name_12c FROM t GROUP BY type ORDER BY type ;