mysql 分組統計 及 統計結果橫向展示
阿新 • • 發佈:2021-02-15
文章目錄
本文主要介紹mysql常用的兩種分組情況:
- 按數值範圍分組,並橫向展示
- 按類別值分組,並橫向展示
PS:為直觀展示,最後橫向展示的欄位名包含中文(真實場景中不推薦使用!!!)
範圍分組
需求說明
- 對總分範圍進行統計,示例如下
總分範圍 range | 總數 |
---|---|
< 60 | 1 |
(60, 70] | 10 |
(70, 80] | 20 |
(80, 90] | 30 |
(90, 100] | 5 |
- 希望將上述分組後的結果,變更為橫向展示,如未及格、D、C、B、A級別各多少人
A | B | C | D | 未及格 |
---|---|---|---|---|
5 | 30 | 20 | 10 | 1 |
示例
- 建立表及資料
CREATE TABLE `t_student` (
`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'PK',
`score` INT(11) NOT NULL DEFAULT '0',
`name` VARCHAR(64) NOT NULL COLLATE 'utf8mb4_unicode_ci',
PRIMARY KEY (`id`) USING BTREE
) COMMENT='學生表' COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB AUTO_INCREMENT =1;
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (1, 58, '王一');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (2, 63, '趙一');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (3, 68, '李一');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (4, 71, '德康');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (5, 77, '古月');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (6, 75, '劉一');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (7, 89, '李二');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (8, 89, '李三');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (9, 88, '李四');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (10, 86, '李武');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (11, 100, '張三');
INSERT INTO `t_student` (`id`, `score`, `name`) VALUES (12, 95, '王五');
- 分組及橫向展示
SELECT
sum(case when `score` < 60 then 1 ELSE 0 END) AS '不及格',
sum(case when `score` < 70 AND `score` >= 60 then 1 ELSE 0 END) AS D,
sum(case when `score` < 80 AND `score` >= 70 then 1 ELSE 0 END) AS C,
sum(case when `score` < 90 AND `score` >= 80 then 1 ELSE 0 END) AS B,
sum(case when `score` <= 100 AND `score` >= 90 then 1 ELSE 0 END) AS A
FROM t_student
sum 與 case when組合
一般使用case when進行範圍區分(簡要分組),再利用count進行統計
類別值分組
需求說明
- 對類別進行統計,示例如下
類別(0:語文;1:數學;2:英語;3:體育) | 報名人數 |
---|---|
0 | 4 |
1 | 3 |
2 | 2 |
3 | 4 |
- 希望將上述分組後的結果,變更為橫向展示,如選擇語文、數學、英語及體育各多少人
語文 | 數學 | 英語 | 體育 |
---|---|---|---|
4 | 3 | 2 | 4 |
示例
- 建立表及資料
CREATE TABLE `t_course` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`type` INT(11) NOT NULL COMMENT '型別(0:語文;1:數學;2:英語;3:體育)',
`student_id` INT(11) NOT NULL COMMENT '學生id',
PRIMARY KEY (`id`) USING BTREE
) COMMENT='課程表' COLLATE='utf8mb4_unicode_ci' ENGINE=InnoDB;
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (1, 0, 1);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (2, 0, 2);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (3, 0, 3);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (4, 0, 4);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (5, 1, 1);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (6, 1, 3);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (7, 2, 1);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (8, 2, 2);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (9, 2, 4);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (10, 3, 1);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (11, 3, 2);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (12, 3, 3);
INSERT INTO `t_course` (`id`, `type`, `student_id`) VALUES (13, 3, 4);
- 用group by 對類別進行分組
SELECT `type`, COUNT(*) AS counts FROM t_course GROUP BY `type`
3. 將分組後的縱向結果,換為橫向展示
SELECT
SUM(case when t.`type` = 0 then t.counts ELSE 0 END) AS '語文',
SUM(case when t.`type` = 1 then t.counts ELSE 0 END) AS '數學',
SUM(case when t.`type` = 2 then t.counts ELSE 0 END) AS '英語',
SUM(case when t.`type` = 3 then t.counts ELSE 0 END) AS '體育'
FROM
(
SELECT
`type`, COUNT(*) AS counts
FROM t_course
GROUP BY `type`
)t
group by 、 sum 與 case when組合
分組統計:
- group by 分組 count 個數
- case when 按條件區分,sum求和