Search code examples
mysqlrollup

MySQL WITH ROLLUP not showing what I expected


I'm trying to add some totals to my SELECT query but I'm struggling to see why this isn't working:

SELECT client, job_type, actual_value_fee FROM jo2details GROUP BY client, job_type WITH ROLLUP

Here's what I expected to see:

+--------+----------+------------------+
| client | job_type | actual_value_fee |
+--------+----------+------------------+
| 110    | 2        | 1250             |
| 110    | 20       | 200              |
| 110    |          | 1450             |
| 228    | 27       | 1000             |
| 228    |          | 1000             |
| 229    | 32       | 0                |
| 229    |          | 0                |
|        |          | 2450             |
+--------+----------+------------------+

But here's what I'm getting:

+--------+----------+------------------+
| client | job_type | actual_value_fee |
+--------+----------+------------------+
| 110    | 2        | 1250             |
| 110    | 20       | 200              |
| 110    |          | 200              |
| 228    | 27       | 1000             |
| 228    |          | 1000             |
| 229    | 32       | 0                |
| 229    |          | 0                |
|        |          | 0                |
+--------+----------+------------------+

Here's the table:

CREATE TABLE `jo2details` (
  `ref` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ID` int(11) unsigned NOT NULL,
  `client` int(11) unsigned NOT NULL,
  `job_type` int(11) unsigned DEFAULT NULL,
  `actual_value_fee` decimal(11,2) DEFAULT NULL,
  `last_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`ref`),
  UNIQUE KEY `ref` (`ref`,`ID`),
  UNIQUE KEY `ref_2` (`ref`,`ID`,`client`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=413 ;

All help much appreciated!


Solution

  • try this:

    SELECT client, job_type, sum(actual_value_fee)
     FROM jo2details 
     GROUP BY client, job_type WITH ROLLUP