Okay. You can also use
CASE WHEN with
COUNT to count selected rows. The secret here is to omit the
ELSE part. Let's try to rewrite the example with payment acceptance using
WHEN pay_date BETWEEN '2015-06-01' AND '2015-06-05'
END) AS accepted_payment,
WHEN pay_date = '2015-06-06'
END) AS conditionally_accepted_payment,
WHEN pay_date > '2015-06-06'
END) AS not_accepted
Let's see what's changed. We don't use
SUM, instead we use
COUNT. The condition after
WHEN stays the same, but now, if the condition is satisfied, we provide the column
pay_date for that particular row so that
COUNT can count it. The
CASE WHEN does not have the
ELSE part, so if the condition is not met,
CASE WHEN returns
NULL, which is not counted by
In this way, we calculate