출처 : http://www.sqlleader.com/mboard.asp?exec=view&strBoardID=SS2005TSQL&intPage=1&intCategory=0&strSearchCategory=|s_name|s_subject|&strSearchWord=&intSeq=972
다음과 같이 같은 id 그룹별로 쉼표(,)로 붙여서 출력하는 문제입니다.
USE TEMPDB GO IF EXISTS (SELECT * FROM SYS.TABLES WHERE NAME = 't1' AND type = 'U') DROP TABLE t1 Go CREATE TABLE t1 (id INT, NAME VARCHAR(MAX)) INSERT t1 values (1,'Jamie') INSERT t1 values (1,'Joe') INSERT t1 values (1,'John') INSERT t1 values (2,'Sai') INSERT t1 values (2,'Sam') INSERT t1 values (3,'Roger') INSERT t1 values (4,'Walter') INSERT t1 values (4,'Teddy') GO SELECT * FROM T1 GO /* id NAME ----------------- 1 Jamie 1 Joe 1 John 2 Sai 2 Sam 3 Roger 4 Walter 4 Teddy */ |
-------------------------------------------------------------------------------------------- -- Nick Barclay -------------------------------------------------------------------------------------------- WITH ConcatNamesCTE (id, [NAME], rn) AS ( SELECT id, [NAME], rn FROM ( SELECT id, [NAME], row_number() OVER(PARTITION BY id ORDER BY id) AS rn FROM t1 ) a WHERE rn = 1 UNION ALL SELECT b.id, cn.[NAME] + ',' + b.[NAME], b.rn FROM ( SELECT id, [NAME], row_number() OVER(PARTITION BY id ORDER BY id) AS rn FROM t1 ) b INNER JOIN ConcatNamesCTE cn ON cn.id = b.id AND cn.rn + 1 = b.rn ) SELECT d.id, d.[NAME] FROM ( SELECT MAX(rn) AS rn, id FROM ConcatNamesCTE GROUP BY id ) c INNER JOIN ConcatNamesCTE d ON d.id = c.id AND d.rn = c.rn ORDER BY id GO |
-------------------------------------------------------------------------------------------- --Adrian Downes -------------------------------------------------------------------------------------------- SELECT res.id, MAX(res.[NAME]) AS [NAME] FROM ( SELECT c.id, CASE WHEN PATINDEX('%' + d.[NAME] + '%', c.[NAME]) = 0 AND c.id = d.id THEN c.[NAME] + ', ' + d.[NAME] ELSE c.[NAME] END AS [NAME] FROM ( SELECT a.id, MIN(a.[NAME]) AS [NAME] FROM ( SELECT y.id, CASE WHEN PATINDEX('%' + z.[NAME] + '%', y.[NAME]) = 0 AND y.id = z.id THEN y.[NAME] + ', ' + z.[NAME] END AS [NAME] FROM t1 y INNER JOIN t1 z ON y.id = z.id ) a GROUP BY a.id ) c INNER JOIN t1 d ON c.id = d.id ) res GROUP BY res.id GO |
-------------------------------------------------------------------------------------------- --Jamie Hunter -------------------------------------------------------------------------------------------- SELECT DISTINCT id , STUFF( (SELECT ',' + name AS [text()] FROM t1 b WHERE b.id = a.id FOR XML PATH('')) , 1, 1, '' ) AS name_csv FROM t1 a ORDER BY 1 GO |
-------------------------------------------------------------------------------------------- --Rick R -------------------------------------------------------------------------------------------- WITH t2 AS ( SELECT id, MIN(name) name FROM t1 GROUP BY id UNION ALL SELECT a.id, a.[NAME] + ',' + b.[NAME] AS name FROM t1 a JOIN t2 b ON a.id = b.id AND a.[NAME] > b.[NAME] ) SELECT id, MAX(name) FROM t2 GROUP BY id GO |
'MSSQL' 카테고리의 다른 글
MSSQL identity 테스트 (0) | 2012.01.20 |
---|---|
(펌) MSSQL Identity 자동 증가함수 (0) | 2012.01.20 |
T-SQL) group by 결과를 가로로 출력 (0) | 2011.12.27 |
(펌) MSSQL Server 백업 / 복구 시나리오 (0) | 2011.10.20 |
(펌) MSSQL 2005 백업계획작성 (0) | 2011.10.20 |