Hi,
I have a table called users
with a column called activated_at
, and I want to count how many users have been activated by checking if the column is null or not. And then display them side by side like this:
+----------+-----------+---------------+-------+
| Malaysia | Activated | Not Activated | Total |
+----------+-----------+---------------+-------+
| Malaysia | 5487 | 303 | 5790 |
+----------+-----------+---------------+-------+
So this is my SQL:
select "Malaysia",
(select count(*) from users where activated_at is not null and locale='en' and date_format(created_at,'%m')=date_format(now(),'%m')) as "Activated",
(select count(*) from users where activated_at is null and locale='en' and date_format(created_at,'%m')=date_format(now(),'%m')) as "Not Activated",
count(*) as "Total"
from users
where locale="en"
and date_format(created_at,'%m')=date_format(now(),'%m');
In my code, I have to specify all the where statements three times, which is obviously redundant. How can I refactor this?
Regards, MK.