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

Time:08-15

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.

CodePudding user response:

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