+----+-------+-------+
| id | style | color |
+----+-------+-------+
| 1 | 1 | red |
| 2 | 1 | blue |
| 3 | 2 | red |
| 4 | 2 | blue |
| 5 | 2 | green |
| 6 | 3 | blue |
+----+-------+-------+
The query:
SELECT style, COUNT(*) as count from t GROUP BY style WITH ROLLUP HAVING count > 1;
produces:
+-------+-------+
| style | count |
+-------+-------+
| 1 | 2 |
| 2 | 3 |
| NULL | 6 |
+-------+-------+
What do I have to do to get WITH ROLLUP to sum only those counts meeting the HAVING requirement? That is, I’d like to see ‘5’ for count in the rollup row.
This was totally convoluted and nasty but I got it
Here is the sample data from the question:
Here it is loaded:
Here is the query’s result:
The problem is that
WITH ROLLUPis evaluated beforeHAVING. I arranged the query in such a way thatWITH ROLLUPwas done last.Mission Accomplished !!!