sql server - SQL - Distinct on two columns and summarized values on third -


I have a table with 3 different IDs I want to isolate columns 1 and 2 and the values ​​of column 3 I want to summarize (in a field - perhaps a different string than comma). Summary area is not "good looking" (no problem: '4,3,' (finally comma)). I am using MS SQL Server 2008.

Example:

  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 ...?

Result:

  ID1 ID2 summary 1 1 5 5 1 2 5, 8 2 3 10,11 2 5 12  

Edit - In the third column deleted

It is said that the field does not look good in a nutshell, try it:

  SELECT M1.ID1, M1.ID2, (Save Convert (Navkar (50), ID3) + ' , 'MyTable M2 to WHERE M1.ID1 = M2.ID1 and M1. ID2 = M 2. For ID2 XML Path (' ')) Matabable M1 Group M1 to M1 .ID1, M1.ID2 < / Code> 

In this Variation that table name is the result of a comma in called 'MyTable' is the query at the end, but you should start. Using my test database, this output was:

  ID1 ID2M1 1 5,8, 1 2 5, 8, 2 3 10, 11, 2 5 12,  

If you want to clear it, then it should work (though the query itself is ugly):

  SELECT ID1, ID2, left (M1, len (M1) - 1) as M1 (Selection M.ID1, M.Id2, (Convert Convert (Encyclopedia (50), ID3) + ', Matabable M2 Where M.ID1 = M 2. ID1 and M.ID2 = M 2. Path for ID2 XML ('')) by Maitabel M1 Group Out of Lkaidi 1, M Lkaidi 2m 1) Clean up  

Comments