Search code examples
mysqlgroup-byhavingrollup

rollup ignores group by having constraint


+----+-------+-------+
| 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.


Solution

  • This was totally convoluted and nasty but I got it

    SELECT style,COUNT(1) as count
    FROM t
    WHERE NOT EXISTS
    (
        SELECT t1.style as count
        FROM
        (
            SELECT style from t GROUP BY style HAVING count(*) = 1
        ) t1 WHERE t.style = t1.style
    )
    GROUP BY style
    WITH ROLLUP;
    

    Here is the sample data from the question:

    drop database if exists rollup_test;
    create database rollup_test;
    use rollup_test
    create table t (id int not null auto_increment,
    style int,color varchar(10),primary key (id));
    insert into t (style,color) values
    (1,'red'),(1,'blue'),(2,'red'),
    (2,'blue'),(2,'green'),(3,'blue');
    select * from t;
    

    Here it is loaded:

    mysql> drop database if exists rollup_test;
    Query OK, 1 row affected (0.07 sec)
    
    mysql> create database rollup_test;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> use rollup_test
    Database changed
    mysql> create table t (id int not null auto_increment,
        -> style int,color varchar(10),primary key (id));
    Query OK, 0 rows affected (0.10 sec)
    
    mysql> insert into t (style,color) values
        -> (1,'red'),(1,'blue'),(2,'red'),
        -> (2,'blue'),(2,'green'),(3,'blue');
    Query OK, 6 rows affected (0.05 sec)
    Records: 6  Duplicates: 0  Warnings: 0
    
    mysql> select * from t;
    +----+-------+-------+
    | id | style | color |
    +----+-------+-------+
    |  1 |     1 | red   |
    |  2 |     1 | blue  |
    |  3 |     2 | red   |
    |  4 |     2 | blue  |
    |  5 |     2 | green |
    |  6 |     3 | blue  |
    +----+-------+-------+
    6 rows in set (0.00 sec)
    
    mysql>
    

    Here is the query's result:

    mysql> SELECT style,COUNT(1) as count
        -> FROM t
        -> WHERE NOT EXISTS
        -> (
        ->     SELECT t1.style as count
        ->     FROM
        ->     (
        ->         SELECT style from t GROUP BY style HAVING count(*) = 1
        ->     ) t1 WHERE t.style = t1.style
        -> )
        -> GROUP BY style
        -> WITH ROLLUP;
    +-------+-------+
    | style | count |
    +-------+-------+
    |     1 |     2 |
    |     2 |     3 |
    |  NULL |     5 |
    +-------+-------+
    3 rows in set (0.00 sec)
    
    mysql>
    

    The problem is that WITH ROLLUP is evaluated before HAVING. I arranged the query in such a way that WITH ROLLUP was done last.

    Mission Accomplished !!!