-2

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

2
  • Why no Brand2 in the 3rd group?
    – dnoeth
    Aug 24, 2016 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, 2016 at 16:48

1 Answer 1

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.