Performing a WITH ROLLUP
when grouping by multiple fields, MySQL returns a rollup row for each group, as well as the overall summary:
CREATE TAB
Try to use a subquery, e.g. -
SELECT * FROM (
SELECT name, number, COUNT(1) FROM test GROUP BY name, number WITH ROLLUP) t
WHERE name IS NULL OR number IS NULL
You also may want to change NULL values with appropriate texts.
SELECT COALESCE(name, 'TOTAL') as Name, number, COUNT(1) FROM test GROUP BY name, number WITH ROLLUP;
Below Name column it would Display as Total. If you have issue with number as null same can be done for that too.
HAVING can do the trick with no subquery:
SELECT `name`, number, COUNT(1) FROM test GROUP BY `name`, number WITH ROLLUP
HAVING number IS NOT NULL OR `name` IS NULL;
This filters out the post-rollup rows except for the grand total:
name number COUNT(1)
------ ------ --------
bar 1 1
bar 2 4
bar 3 1
baz 1 1
baz 2 1
foo 1 2
foo 2 1
foo 3 2
(NULL) (NULL) 13