1SELECT emp_id, fname, lname, dept_id,
2GROUP_CONCAT ( strength ) as "strengths"
3FROM employee group by emp_id;
4
1STRING_AGG ( expression, separator ) [ <order_clause> ]
2
3<order_clause> ::=
4 WITHIN GROUP ( ORDER BY <order_by_expression_list> [ ASC | DESC ] )
5 SELECT STRING_AGG(Genre, ',') AS Result
6FROM Genres;
7Result:
8
9Result
10--------------------------------------------
11Rock,Jazz,Country,Pop,Blues,Hip Hop,Rap,Punk
1CREATE TABLE #YourTable ([ID] INT, [Name] CHAR(1), [Value] INT)
2
3INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,'A',4)
4INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (1,'B',8)
5INSERT INTO #YourTable ([ID],[Name],[Value]) VALUES (2,'C',9)
6
7SELECT
8 [ID],
9 STUFF((
10 SELECT ', ' + [Name] + ':' + CAST([Value] AS VARCHAR(MAX))
11 FROM #YourTable
12 WHERE (ID = Results.ID)
13 FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
14 ,1,2,'') AS NameValues
15FROM #YourTable Results
16GROUP BY ID
17
18DROP TABLE #YourTable