I'm interested in the rollups for fields number and perc in dotable.
So you need to do a sum()
to make it work.
If you don't need the overall total we can also remove it.
For Version <= 5.7
Query:
select
if(`type` is null, 'tot', `rdt`) as `rdt`,
`type`,
`number`,
`total`,
`perc`,
`thedate`
from
(select
`rdt`,
`type`,
sum(`number`) as `number`,
`total`,
sum(`perc`) as `perc`,
`thedate`
from
`dotable`
group by `rdt`, `type` with rollup) as t
where
`rdt` is not null;
Result:
rdt | type | number | total | perc | thedate :-- | :------------ | -----: | ----: | ----: | :--------- MAL | checking | 112 | 3249 | 3.40 | 2014-03-27 MAL | control | 33 | 3249 | 1.00 | 2014-03-27 MAL | reconstructed | 5 | 3249 | 0.20 | 2014-03-27 MAL | regular | 960 | 3249 | 29.50 | 2014-03-27 MAL | study | 10 | 3249 | 0.30 | 2014-03-27 tot | null | 1120 | 3249 | 34.40 | 2014-03-27 PMC | checking | 107 | 3153 | 3.40 | 2014-03-27 PMC | control | 167 | 3153 | 5.30 | 2014-03-27 PMC | reconstructed | 8 | 3153 | 0.30 | 2014-03-27 PMC | regular | 833 | 3153 | 26.40 | 2014-03-27 PMC | study | 72 | 3153 | 2.30 | 2014-03-27 tot | null | 1187 | 3153 | 37.70 | 2014-03-27 RAS | checking | 1 | 970 | 0.10 | 2014-03-27 RAS | control | 42 | 970 | 4.30 | 2014-03-27 RAS | reconstructed | 1 | 970 | 0.10 | 2014-03-27 RAS | regular | 318 | 970 | 32.80 | 2014-03-27 RAS | study | 3 | 970 | 0.30 | 2014-03-27 tot | null | 365 | 970 | 37.60 | 2014-03-27 UOT | checking | 11 | 3527 | 0.30 | 2014-03-27 UOT | control | 283 | 3527 | 8.00 | 2014-03-27 UOT | regular | 235 | 3527 | 6.70 | 2014-03-27 UOT | study | 8 | 3527 | 0.20 | 2014-03-27 tot | null | 537 | 3527 | 15.20 | 2014-03-27
For Version > 5.7
Query:
select
if(`type` is null, 'tot', `rdt`) as `rdt`,
`type`,
sum(`number`) as `number`,
`total`,
sum(`perc`) as `perc`,
`thedate`
from
`dotable`
group by `rdt`, `type` with rollup
having not grouping(rdt) <> 0;
Result:
rdt | type | number | total | perc | thedate :-- | :------------ | -----: | ----: | ----: | :--------- MAL | checking | 112 | 3249 | 3.40 | 2014-03-27 MAL | control | 33 | 3249 | 1.00 | 2014-03-27 MAL | reconstructed | 5 | 3249 | 0.20 | 2014-03-27 MAL | regular | 960 | 3249 | 29.50 | 2014-03-27 MAL | study | 10 | 3249 | 0.30 | 2014-03-27 tot | null | 1120 | 3249 | 34.40 | 2014-03-27 PMC | checking | 107 | 3153 | 3.40 | 2014-03-27 PMC | control | 167 | 3153 | 5.30 | 2014-03-27 PMC | reconstructed | 8 | 3153 | 0.30 | 2014-03-27 PMC | regular | 833 | 3153 | 26.40 | 2014-03-27 PMC | study | 72 | 3153 | 2.30 | 2014-03-27 tot | null | 1187 | 3153 | 37.70 | 2014-03-27 RAS | checking | 1 | 970 | 0.10 | 2014-03-27 RAS | control | 42 | 970 | 4.30 | 2014-03-27 RAS | reconstructed | 1 | 970 | 0.10 | 2014-03-27 RAS | regular | 318 | 970 | 32.80 | 2014-03-27 RAS | study | 3 | 970 | 0.30 | 2014-03-27 tot | null | 365 | 970 | 37.60 | 2014-03-27 UOT | checking | 11 | 3527 | 0.30 | 2014-03-27 UOT | control | 283 | 3527 | 8.00 | 2014-03-27 UOT | regular | 235 | 3527 | 6.70 | 2014-03-27 UOT | study | 8 | 3527 | 0.20 | 2014-03-27 tot | null | 537 | 3527 | 15.20 | 2014-03-27
Fiddle:
db<>fiddle here
Note: When using group by
without ERROR 1055 you need to set as
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));