在按多个字段分组时执行WITH ROLLUP,MySQL为每个组返回一个汇总行。 我对下面的dotable字段编号和perc的汇总感兴趣:
+------+---------------+--------+-------+--------+------------+
| RDT | type | number | total | perc | thedate |
+------+---------------+--------+-------+--------+------------+
| MAL | checking | 112 | 3249 | 3.4 | 2014-03-27 |
| MAL | control | 33 | 3249 | 1.0 | 2014-03-27 |
| MAL | reconstructed | 5 | 3249 | 0.2 | 2014-03-27 |
| MAL | regular | 960 | 3249 | 29.5 | 2014-03-27 |
| MAL | study | 10 | 3249 | 0.3 | 2014-03-27 |
| tot | NULL | 1120 | 3249 | 34.0 | 2014-03-27 |
| PMC | checking | 107 | 3153 | 3.4 | 2014-03-27 |
| PMC | control | 167 | 3153 | 5.3 | 2014-03-27 |
| PMC | reconstructed | 8 | 3153 | 0.3 | 2014-03-27 |
| PMC | regular | 833 | 3153 | 26.4 | 2014-03-27 |
| PMC | study | 72 | 3153 | 2.3 | 2014-03-27 |
| tot | NULL | 1187 | 3249 | 36.5 | 2014-03-27 |
| RAS | checking | 1 | 970 | 0.1 | 2014-03-27 |
| RAS | control | 42 | 970 | 4.3 | 2014-03-27 |
| RAS | reconstructed | 1 | 970 | 0.1 | 2014-03-27 |
| RAS | regular | 318 | 970 | 32.8 | 2014-03-27 |
| RAS | study | 3 | 970 | 0.3 | 2014-03-27 |
| tot | NULL | 365 | 970 | 37.6 | 2014-03-27 |
| UOT | checking | 11 | 3527 | 0.3 | 2014-03-27 |
| UOT | control | 283 | 3527 | 8.0 | 2014-03-27 |
| UOT | regular | 235 | 3527 | 6.7 | 2014-03-27 |
| UOT | study | 8 | 3527 | 0.2 | 2014-03-27 |
| tot | NULL | 537 | 3527 | 15.2 | 2014-03-27 |
+------+---------------+--------+-------+--------+------------+
我尝试了这个问题:
mysql> SELECT
IFNULL(`RDT`, 'tot') AS RDT,
`type`,
`number`,
`total`,
`perc`,
`thedate`
FROM
(
SELECT
`RDT`,
`type`,
`NUMBER`,
`total`,
`perc`,
`THEDATE`
FROM
`dotable`
GROUP BY
RDT,
`type`
ORDER BY
CASE
WHEN RDT = 'UOT' THEN
1
END ASC
) AS X
GROUP BY
X.RDT,
X.`type` WITH ROLLUP;
但输出错了,你能帮帮我吗? 可点击的结构和行在下面。
+------+---------------+--------+-------+--------+------------+
| RDT | type | number | total | perc | thedate |
+------+---------------+--------+-------+--------+------------+
| MAL | checking | 112 | 3249 | 3.4 | 2014-03-27 |
| MAL | control | 33 | 3249 | 1.0 | 2014-03-27 |
| MAL | reconstructed | 5 | 3249 | 0.2 | 2014-03-27 |
| MAL | regular | 960 | 3249 | 29.5 | 2014-03-27 |
| MAL | study | 10 | 3249 | 0.3 | 2014-03-27 |
| MAL | NULL | 10 | 3249 | 0.3 | 2014-03-27 |
| PMC | checking | 107 | 3153 | 3.4 | 2014-03-27 |
| PMC | control | 167 | 3153 | 5.3 | 2014-03-27 |
| PMC | reconstructed | 8 | 3153 | 0.3 | 2014-03-27 |
| PMC | regular | 833 | 3153 | 26.4 | 2014-03-27 |
| PMC | study | 72 | 3153 | 2.3 | 2014-03-27 |
| PMC | NULL | 72 | 3153 | 2.3 | 2014-03-27 |
| RAS | checking | 1 | 970 | 0.1 | 2014-03-27 |
| RAS | control | 42 | 970 | 4.3 | 2014-03-27 |
| RAS | reconstructed | 1 | 970 | 0.1 | 2014-03-27 |
| RAS | regular | 318 | 970 | 32.8 | 2014-03-27 |
| RAS | study | 3 | 970 | 0.3 | 2014-03-27 |
| RAS | NULL | 3 | 970 | 0.3 | 2014-03-27 |
| UOT | checking | 11 | 3527 | 0.3 | 2014-03-27 |
| UOT | control | 283 | 3527 | 8.0 | 2014-03-27 |
| UOT | regular | 235 | 3527 | 6.7 | 2014-03-27 |
| UOT | study | 8 | 3527 | 0.2 | 2014-03-27 |
| UOT | NULL | 8 | 3527 | 0.2 | 2014-03-27 |
| tot | NULL | 8 | 3527 | 0.2 | 2014-03-27 |
+------+---------------+--------+-------+--------+------------+
24 rows in set
mysql>
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `dotable`
-- ----------------------------
DROP TABLE IF EXISTS `dotable`;
CREATE TABLE `dotable` (
`RDT` varchar(10) DEFAULT NULL,
`TYPE` varchar(50) DEFAULT NULL,
`NUMBER` int(11) NOT NULL DEFAULT '0',
`TOTAL` int(11) NOT NULL DEFAULT '0',
`PERC` decimal(10,2) DEFAULT NULL,
`THEDATE` date DEFAULT NULL,
`ID` int(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=20 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of dotable
-- ----------------------------
INSERT INTO `dotable` VALUES ('UOT', 'control', '283', '3527', '8.0', '2014-03-27', '1');
INSERT INTO `dotable` VALUES ('UOT', 'regular', '235', '3527', '6.7', '2014-03-27', '2');
INSERT INTO `dotable` VALUES ('UOT', 'study', '8', '3527', '0.2', '2014-03-27', '3');
INSERT INTO `dotable` VALUES ('UOT', 'checking', '11', '3527', '0.3', '2014-03-27', '4');
INSERT INTO `dotable` VALUES ('MAL', 'regular', '960', '3249', '29.5', '2014-03-27', '5');
INSERT INTO `dotable` VALUES ('MAL', 'study', '10', '3249', '0.3', '2014-03-27', '6');
INSERT INTO `dotable` VALUES ('MAL', 'control', '33', '3249', '1.0', '2014-03-27', '7');
INSERT INTO `dotable` VALUES ('MAL', 'reconstructed', '5', '3249', '0.2', '2014-03-27', '8');
INSERT INTO `dotable` VALUES ('MAL', 'checking', '112', '3249', '3.4', '2014-03-27', '9');
INSERT INTO `dotable` VALUES ('PMC', 'regular', '833', '3153', '26.4', '2014-03-27', '10');
INSERT INTO `dotable` VALUES ('PMC', 'study', '72', '3153', '2.3', '2014-03-27', '11');
INSERT INTO `dotable` VALUES ('PMC', 'control', '167', '3153', '5.3', '2014-03-27', '12');
INSERT INTO `dotable` VALUES ('PMC', 'checking', '107', '3153', '3.4', '2014-03-27', '13');
INSERT INTO `dotable` VALUES ('PMC', 'reconstructed', '8', '3153', '0.3', '2014-03-27', '14');
INSERT INTO `dotable` VALUES ('RAS', 'reconstructed', '1', '970', '0.1', '2014-03-27', '15');
INSERT INTO `dotable` VALUES ('RAS', 'regular', '318', '970', '32.8', '2014-03-27', '16');
INSERT INTO `dotable` VALUES ('RAS', 'study', '3', '970', '0.3', '2014-03-27', '17');
INSERT INTO `dotable` VALUES ('RAS', 'control', '42', '970', '4.3', '2014-03-27', '18');
INSERT INTO `dotable` VALUES ('RAS', 'checking', '1', '970', '0.1', '2014-03-27', '19');
答案 0 :(得分:0)
我对dotable中的字段号和perc汇总感兴趣。
因此,您需要执行pm2 stop all
sudo certbot renew
pm2 start all
才能使其正常工作。
如果您不需要总计,我们也可以将其删除。
对于版本<= 5.7
查询:
sum()
结果:
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
对于版本> 5.7
查询:
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;
结果:
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
小提琴:
db <>提琴here
注意:当使用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;
而没有 ERROR 1055 时,您需要设置为
group by