views:

58

answers:

1

Hi,

I'm sure this has been asked but I can't quite find the right search terms.

Given a schema like this:

| CarMakeID | CarMake
------------------------
|         1 | SuperCars
|         2 | MehCars

| CarMakeID | CarModelID | CarModel
-----------------------------------------
|         1 |          1 | Zoom
|         2 |          1 | Wow
|         3 |          1 | Awesome
|         4 |          2 | Mediocrity
|         5 |          2 | YoureSettling

I want to produce a dataset like this:

| CarMakeID | CarMake   | CarModels
---------------------------------------------
|         1 | SuperCars | Zoom, Wow, Awesome
|         2 | MehCars   | Mediocrity, YoureSettling

What do I do in place of 'AGG' for strings in SQL Server in the following style query?

SELECT *, 
 (SELECT AGG(CarModel) 
  FROM CarModels model
  WHERE model.CarMakeID = make.CarMakeID
  GROUP BY make.CarMakeID) as CarMakes
FROM CarMakes make
+2  A: 

http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

gbn
Wow non-trivial.
Graphain
The article needs an update - the CLR was for SQL Server 2005. 2008 supports multiple parameters in an aggregate function, so you can recreate MySQL's GROUP_CONCAT's parameters.
OMG Ponies