Get distinct values from a column in a single comma separated string in sql?

Viewed 28

I have a table that has rows that contains data for the same address from different sources-

RowID Address Source
1 Addr1 Src 1
1 Addr1 Src 2
1 Addr1 Src 3

What I want to achieve -

RowID Address Source
1 Addr1 Src 1, Src2, Src3

I am using snowflake.

1 Answers

ou can use listagg to achieve it.

If ou need a special order, you need to add following after the listagg

WITHIN GROUP (ORDER BY sortcolumn DESC)

The query without ordering is

SELECT
    RowID,  Address, listagg(Source, ', ')
FROM table1
GROUP BY RowID  Address
Related