Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading