Code used in the video is pasted below.
SELECT *
FROM "EXAMPLE_DB"."EXAMPLE_SCHEMA"."EXAMPLE_TABLE"
PIVOT(sum("VALUE") for
CATEGORY in ('AB', 'BA', 'AC', 'CA'))
;
SET column_list = (
SELECT LISTAGG(DISTINCT "CATEGORY", '\',\'')
FROM "EXAMPLE_DB"."EXAMPLE_SCHEMA"."EXAMPLE_TABLE"
WHERE CONTAINS("CATEGORY", 'A')
);
SELECT $column_list;
SET column_list2 = concat('(\'', $column_list, '\'))');
SELECT $column_list2;
set sql_query = concat('SELECT * FROM "EXAMPLE_DB"."EXAMPLE_SCHEMA"."EXAMPLE_TABLE" PIVOT(sum("VALUE") for CATEGORY in', $column_list2);
SELECT $sql_query;
EXECUTE IMMEDIATE $sql_query
Watch video Use Snowflake’s Pivot Function with a Dynamic List of Columns online without registration, duration hours minute second in high quality. This video was added by user DotPi 16 July 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 12,33 once and liked it 11 people.