I have a table with 3 different IDs. I want to distinct on column 1 and 2 and summarize the values of column 3 (into one field – maybe comma separated string). The summarized field doesn’t have to “look nice” (no problems with: ‘4,3,’ (comma at the end)).
I’m using MS SQL Server 2008.
E.g:
ID1 ID2 ID3
1 1 5
1 1 8
1 2 5
1 2 8
2 3 10
2 3 11
2 5 12
SELECT …?
The result:
ID1 ID2 Summary
1 1 5,8
1 2 5,8
2 3 10,11
2 5 12
Edit – removed trailing spaces in the third column
Since you said the summarized field doesn’t have to look nice, try this:
It assumes your table name is called ‘MyTable’. This query results in commas at the end, but should get you started. Using my test database, this was the output:
If you want it cleaned up, this should work (although the query itself is ugly):