Skip to content
Advertisement

Snowflake: ListAgg data based on condition

I have a table P that looks something like this:

Here, 1 signifies test success whereas any other number signify some kind of failure. I want to aggregate this data in such a way that for every failed test I have a comma separated list of failed environments in every row. And if there are no failures, there should be NULL in the new columns. The output would look something like

I am using the snowflake LISTAGG() function in a query like

This is the output I get:

How can I change this query to get the output that I am looking for?

Advertisement

Answer

You could solve this with a correlated subquery:

Or better yet using listagg() as a window function, which Snowflake supports:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement