-2

This question already has an answer here:

I have the below table and i would like to get the below result with using the COALESCE . Any help will be appreciated.

Coleasce

Thanks sam

marked as duplicate by Martin Smith, Kin Shah, Mark Sinkinson, Andriy M, mustaccio Aug 24 '16 at 17:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Why no Brand2 in the 3rd group? – dnoeth Aug 24 '16 at 16:48
  • 1
    Why do you want to use COALESCE for this? Do you know what that function actually does? – Andriy M Aug 24 '16 at 16:48
2

This is the generally accepted standard for how to concatenate values contained over multiple rows.

SELECT
    ID,
    LEFT(brands, LEN(brands) -2) brands
FROM 
    (
        SELECT DISTINCT ID, 
            (
                SELECT
                    brand + ', ' [text()]
                FROM 
                    myTable brands 
                WHERE 
                    brands.id = ids.id
                FOR XML PATH ('')
            ) brands
        FROM
            myTable ids
        GROUP BY 
            ID
    ) m

Of course adding coalesce (null, value) for any given value will meet your criteria for using the coalesce function but this is doesn't add functionality or leverage the coalesce function in a useful way.

Not the answer you're looking for? Browse other questions tagged or ask your own question.