Amit
Amit

Reputation: 11

convert multiple rows to one comma separated values in snowflake

I need to convert multiple records/rows in a column to a single comma separated values in snowflake. I was using FOR XML in MSSQL SERVER for the same, but I need to do the same in SNOWSQL.

Example - Column-1 with three values A, B, C Column-1 A B C

I need the values concatenated like A,B,C.

Upvotes: 0

Views: 4403

Answers (1)

Rich Murnane
Rich Murnane

Reputation: 2920

Please take a look at Snowflake's LISTAGG function:

https://docs.snowflake.com/en/sql-reference/functions/listagg.html

example as follows:

CREATE OR REPLACE TABLE xyz (str varchar(100));
INSERT INTO xyz (str) VALUES ('A'), ('B'), ('C');
SELECT listagg(str, ',') as my_strings FROM xyz;

--results
MY_STRINGS
A,B,C

I hope this helps...Rich

Upvotes: 2

Related Questions