Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat groups in SQL Server [duplicate]

If I have a table like this:

+------------+
| Id | Value |
+------------+
| 1  | 'A'   |
|------------|
| 1  | 'B'   |
|------------|
| 2  | 'C'   |
+------------+

How can I get a resultset like this:

+------------+
| Id | Value |
+------------+
| 1  | 'AB'  |
|------------|
| 2  | 'C'   |
+------------+

I know this is really easy to do in MySQL using GROUP_CONCAT, but I need to be able to do it in MSSQL 2005

Thanks

(Duplicate of How to use GROUP BY to concatenate strings in SQL Server?)

like image 516
Daniel Moore Avatar asked Jun 02 '09 18:06

Daniel Moore


1 Answers

For a clean and efficient solution you can create an user defined aggregate function, there is even an example that does just what you need.
You can then use it like any other aggregate function (with a standard query plan):

query plan

like image 197
Pent Ploompuu Avatar answered Oct 21 '22 09:10

Pent Ploompuu