0

I'd like to flatten a table of customers and products into list of customers and a corresponding comma separated list of products.

It's in SQL server and I know I can write a simple c# program to do this but I'd prefer a straight tsql solution.

I'm starting like this:

Customer Product
Joe Product1
Joe Product2
Jim Product1
Jill Product3
Jill Product4

And I want to end up like this:

Customer Products
Joe Product1,Product2
Jim Product1
Jill Product3, Product4

I don't want to use PIVOT because I don't want to hard code the productIDs. I'd prefer not to use cursors but if that's the only way I suppose it's better than mixing c# and TSQL.

John Hoge
  • 2,371
  • 2
  • 20
  • 24
  • You don't want to do this... because it makes your data unnormalized. But f you must... [see this answer](https://stackoverflow.com/questions/17591490/how-to-make-a-query-with-group-concat-in-sql-server). – S3S May 31 '19 at 20:43
  • Thanks for the answer! I don't want to store the data that way, just prepare it to send to an external destination. – John Hoge May 31 '19 at 21:00

0 Answers0