HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

What does Then 1 else 0 mean in a CASE expression used with an aggregate function?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
caseexpressionelsewhatwithusedmeanfunctiondoesthen

Problem

SUM(CASE WHEN column1 = 'value1' THEN 1 ELSE 0 END),
SUM(CASE WHEN column2 = 'value2' THEN 1 ELSE 0 END)..


I don't know why I am finding it difficult to understand what exactly is happening in the above statement and what exactly is 'Then 1 else 0' doing here.

Solution

If column1 contains the value value1 then the CASE expression will return 1, and SUM() will add 1 for that row. If it doesn't, the CASE expression will return 0, and it will add 0 for that row. This is a way to count how many rows have value1 in column1, but there are other ways to do this too, e.g. on 2012+:

SELECT COUNT(IIF(column1 = 'value1', 'truish', NULL))


On earlier versions:

SELECT COUNT(CASE WHEN column1 = 'value1' THEN 'truish' END)


(You don't need the ELSE here because, unlike SUM, COUNT ignores NULL.)

And I updated the value 1 to be a string to demonstrate that this has nothing to do with boolean.

Code Snippets

SELECT COUNT(IIF(column1 = 'value1', 'truish', NULL))
SELECT COUNT(CASE WHEN column1 = 'value1' THEN 'truish' END)

Context

StackExchange Database Administrators Q#120003, answer score: 14

Revisions (0)

No revisions yet.