Consider the data below.
account product sales
account1 prod1 10.0
account1 prod1 15.0
account1 prod1 12.0
account1 prod1 5.0
account1 prod1 78.0
account1 prod1 45.0
account1 prod1 34.0
account1 prod1 9.0
account1 prod1 19.0
account1 prod2 12.0
account1 prod2 11.0
account1 prod2 14.4
account1 prod2 6.0
account1 prod2 93.6
account1 prod2 54.0
account1 prod2 25.0
account1 prod2 7.0
account1 prod2 22.8
account2 prod3 23.0
Now i want to iterate through the unique products and want to return the comma separated list of sales for unique products.
I want the output to be like
i.e
Prod1
12.0, 16.0, 45.0, 38.0, 69.0, 42.0, 45.0, 65.0, 28.0, 95.0, 35.0, 25.0
Prod2
12.0, 16.0, 45.0, 38.0, 69.0, 42.0, 45.0, 65.0, 28.0, 95.0, 35.0, 25.0
where this 2nd row is the comma separated list of sales for that product.
What is the best way to achieve this in sql server 2008r2 ?
Should i use cursor ?