Harri
Harri

Reputation: 7

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?

Upvotes: 0

Views: 5331

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 173046

how do I sum X when Y is TRUE only?

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

Upvotes: 1

Related Questions