Skip to content
Advertisement

CASE to sum column based on boolean

I have:

SELECT 
 SUM(X) AS columnA,

CASE WHEN Y = TRUE THEN 'VAR' ELSE 'VAR2' END

Then I group by to get the counts but how do I sum X when Y is TRUE only? Or get the % of A in relation to true/untrue?

Advertisement

Answer

how do I sum X when Y is TRUE only?

SELECT SUM(CASE WHEN Y = TRUE THEN X ELSE 0 END)

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