SQL/Dates & text 8 min
CASE and conditional aggregation
Bucket values inline, then count each bucket in one pass.
casepivotconditional
CASE is SQL's if/else. It walks the WHEN branches in order and returns the first match, falling back to ELSE.
sql · editable
loading editor…
Try it: Move the premium threshold down to 300.
Putting CASE inside an aggregate is the pivot trick: each expression counts or sums only the rows matching its condition, so you get one column per category in a single scan.
sql · editable
loading editor…
The same pattern gives you rates: divide a conditional count by the total count.
sql · editable
loading editor…
practice this