Correct, nice! Now, let's use
SUM in another way. Take a look:
SUM(CASE WHEN pay_date BETWEEN '2015-06-01' AND
'2015-06-05' THEN 1 ELSE 0 END)
SUM(CASE WHEN pay_date = '2015-06-06' THEN 1
ELSE 0 END) AS conditionally_accepted_payment,
SUM(CASE WHEN pay_date > '2015-06-06'
THEN 1 ELSE 0 END) AS not_accepted
In the above query, we start with calculating the number of accepted payments.
Inside the function
SUM, we use a
CASE WHEN construction. It checks whether the
pay_date in a given row is within the deadline specified. If it is, this row is given the number 1 (which means the sum count will go up by 1). If this is not the case, the number is 0 and this row will not affect the sum (e.g., 10 plus 0 is still 10).
The other two
SUM functions (
not_accepted) work in the same way.