Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6997699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:18:59+00:00 2026-05-27T20:18:59+00:00

+—-+——-+——-+ | id | style | color | +—-+——-+——-+ | 1 | 1 |

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:19:00+00:00Added an answer on May 27, 2026 at 8:19 pm

    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 !!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app uses a WebRequest at certain points to get pages from itself. This
I am currently running into a problem where an element is coming back from
— or — Is there a difference between these? Is one better-supported than the
We're building an app, our first using Rails 3, and we're having to build
I'm using a DatePicker ( org.apache.wicket.extensions.yui.calendar.DatePicker — Javadoc ) in a form. The form
What is the absolute fastest way to read and write strings from a file
I'm having a simple problem toggling between +/- characters which prepend a link which
I'm including a local class that requests a file from a remote server. This
string formIdList = 8256, 8258, 8362, 8120, 8270, 8271, 8272, 8273, 8257, 8279, 8212,
I'm using some of Firefox's specially-defined values for cursor, in particular -moz-zoom-in -moz-zoom-out -moz-grab

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.