Skip to content
Advertisement

How to add all text in a column into one cell in Snowflake

So I have data as follows:

ID   tags
001  apple, banana
001  NA
002  berry, blue, banana
003  melon, apple, grape
002  grape
001  apple, banana
001  grape

All I would like to do is make a new table which gathers all text into one cell as follows:

ID   tag_full
001  apple, banana, apple, banana, grape
002  berry, blue, banana, grape
003  melon, apple, grape

So all values of tags are in one cell for each ID. Order does not matter, but they must be separated by commas.

Advertisement

Answer

how about this :

select id , listagg(tags , ',') as tag_full
from tablename 
where tags <> 'NA'
group by id 
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement