select id, name, 'First Category' as category, count(id) as totalCalls
from missed_call
where name = 'whatever1'
group by name, category
UNION
select id, name, 'Second Category' as category, count(id) as totalCalls
from missed_call
where name = 'whatever2'
group by name, category
order by name ASC, totalCalls DESC
The previous query will not retrieve the records where totalCalls is 0.
So, how can I do to get those records and present totalCalls as 0?
UPDATE: I have tried changing count(id) as totalCalls for IFNULL(count(id), 0) as totalCalls but it doesn’t solve the problem. Perhaps, because count(id) is actually not null, it just does not exist.
If you are unwilling to expand your database schema you can always pretend there is a table:
I dropped id in select because you should not select something you are not grouping on – this is probably MySql.
Check this on Sql Fiddle.