Skip to content
Advertisement

Where column “c” is not null for each group in column “a” – give group “a” a value of 1

Column “C” is a flag indicator. I want to run a query where I can sum column “c” where it is not null. I would like 1 value for each group of column A.

For instance group “Test”, would have a total of 1. Group chance has a total of 1. Group “Play” has a total of 0.

I would like a total number as a result. Please view my code below.

A C
Test apple
Test orange
Test
Test apple
Chance
Chance apple
Chance
Play
Play

Advertisement

Answer

If you want a flag per a if any c has a value, then you can use:

You can actually simplify this using the sign() function:

If you want this per row, then use window functions:

Obviously, if you just want to count the non-NULL values, you would use just count(c) without the case expression.

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