사용자 도구

사이트 도구


기술문서:레퍼런스:쿼리:기본:여러_행_문자열_병합

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
기술문서:레퍼런스:쿼리:기본:여러_행_문자열_병합 [2025/08/08 15:01]
carlito76 만듦
기술문서:레퍼런스:쿼리:기본:여러_행_문자열_병합 [2025/11/26 18:56] (현재)
carlito76
줄 1: 줄 1:
-====== 여러 행 문자열 합치기 ======+{{htmlmetatags> 
 +metatag-description=(여러 행 문자열 병합, GROUP_CONCAT, STRING_AGG, LISTAGG, 데이터베이스, DBMS, SQL, MySQL, MariaDB, MSSQL, Oracle, Tibero, PostgreSQL) 
 +metatag-og:description=(여러 행 문자열 병합, GROUP_CONCAT, STRING_AGG, LISTAGG, 데이터베이스, DBMS, SQL, MySQL, MariaDB, MSSQL, Oracle, Tibero, PostgreSQL) 
 +}} 
 + 
 +====== 여러 행 문자열 합 ====== 
 + 
 +<note tip> 
 +최소 지원 버전은 약간의 차이가 있을 수 있습니다. 
 +</note> 
 + 
 +===== 테이블 명세 ===== 
 +^  물리명  ^  논리명  ^  타입  ^  길이 
 +| 해시태그 | hashtag | 문자형 | 100 | 
 +| 정렬 순서 | sort_order | 숫자형 | | 
 + 
 +===== SQL 스크립트 ===== 
 +<sxh sql; gutter: true; title: DDL;> 
 +CREATE TABLE ex_aggregate ( 
 + hashtag VARCHAR(100) NOT NULL 
 + , sort_order INT NOT NULL 
 +); 
 +</sxh> 
 + 
 +<sxh sql; gutter: true; title: DML;> 
 +INSERT INTO ex_aggregate (hashtag, sort_order) VALUES ('MySQL', 1); 
 +INSERT INTO ex_aggregate (hashtag, sort_order) VALUES ('MariaDB', 2); 
 +INSERT INTO ex_aggregate (hashtag, sort_order) VALUES ('MSSQL', 3); 
 +INSERT INTO ex_aggregate (hashtag, sort_order) VALUES ('Oracle', 4); 
 +INSERT INTO ex_aggregate (hashtag, sort_order) VALUES ('Tibero', 5); 
 +INSERT INTO ex_aggregate (hashtag, sort_order) VALUES ('PostgreSQL', 6); 
 +</sxh> 
 + 
 +===== MySQL/MariaDB ===== 
 +<sxh sql; gutter: true;> 
 +/* 
 + MySQL 4.1 이상/MariaDB 5.1 이상 
 +*/ 
 +SELECT 
 + GROUP_CONCAT(hashtag ORDER BY sort_order ASC SEPARATOR ',') AS hashtag 
 +FROM 
 + ex_aggregate 
 +
 +</sxh> 
 + 
 +===== MSSQL ===== 
 +<sxh sql; gutter: true;> 
 +/* 
 + MSSQL 2005 이상 
 +*/ 
 +SELECT 
 + STUFF( 
 + (SELECT 
 + ',' + hashtag 
 + FROM 
 + ex_aggregate 
 + ORDER BY 
 + sort_order ASC 
 + FOR XML PATH(''
 +
 + , 1, 1, '') AS hashtag; 
 +
 + 
 +/* 
 + MSSQL 2017 이상 
 +*/ 
 +-- 정렬 순서 미보장 
 +SELECT 
 + STRING_AGG(hashtag, ',') AS hashtag 
 +FROM 
 + ex_aggregate 
 +
 + 
 +/* 
 + MSSQL 2022 이상 
 +*/ 
 +SELECT 
 + STRING_AGG(hashtag, ',') WITHIN GROUP (ORDER BY sort_order ASC) AS hashtag  
 +FROM 
 + ex_aggregate 
 +
 +</sxh> 
 + 
 +===== Oracle/Tibero ===== 
 +<sxh sql; gutter: true;> 
 +/* 
 + Oracle 11g 이상/Tibero 3 이상 
 +*/ 
 +SELECT 
 + LISTAGG(hashtag, ',') WITHIN GROUP (ORDER BY sort_order ASC) AS hashtag 
 +FROM 
 + ex_aggregate 
 +
 +</sxh> 
 + 
 +===== PostgreSQL ===== 
 +<sxh sql; gutter: true;> 
 +/* 
 + PostgreSQL 9.0 이상 
 +*/ 
 +SELECT 
 + STRING_AGG(hashtag, ',' ORDER BY sort_order ASC) AS hashtag 
 +FROM 
 + ex_aggregate 
 +
 +</sxh> 
 + 
 +{{tag>"쿼리" "Query" "기본"}}
  
-FIXME