当前位置: 首页 > news >正文

mysql超级聚合with rollup

超级聚合,是在group by的基础上,再次进行聚合。
它再次聚合的列,是select中没有用到聚合函数的列。

文章目录

  • 例子1
  • 解释
  • 例子2
  • 表以及数据


例子1

mysql> SELECT year, country, product, SUM(profit) AS profitFROM salesGROUP BY year, country, product;
+------+---------+------------+--------+
| year | country | product    | profit |
+------+---------+------------+--------+
| 2000 | Finland | Computer   |   1500 |
| 2000 | Finland | Phone      |    100 |
| 2000 | India   | Calculator |    150 |
| 2000 | India   | Computer   |   1200 |
| 2000 | USA     | Calculator |     75 |
| 2000 | USA     | Computer   |   1500 |
| 2001 | Finland | Phone      |     10 |
| 2001 | USA     | Calculator |     50 |
| 2001 | USA     | Computer   |   2700 |
| 2001 | USA     | TV         |    250 |
+------+---------+------------+--------+
mysql> SELECT year, country, product, SUM(profit) AS profitFROM salesGROUP BY year, country, product WITH ROLLUP;
+------+---------+------------+--------+
| year | country | product    | profit |
+------+---------+------------+--------+
| 2000 | Finland | Computer   |   1500 |
| 2000 | Finland | Phone      |    100 |
| 2000 | Finland | NULL       |   1600 |
| 2000 | India   | Calculator |    150 |
| 2000 | India   | Computer   |   1200 |
| 2000 | India   | NULL       |   1350 |
| 2000 | USA     | Calculator |     75 |
| 2000 | USA     | Computer   |   1500 |
| 2000 | USA     | NULL       |   1575 |
| 2000 | NULL    | NULL       |   4525 |
| 2001 | Finland | Phone      |     10 |
| 2001 | Finland | NULL       |     10 |
| 2001 | USA     | Calculator |     50 |
| 2001 | USA     | Computer   |   2700 |
| 2001 | USA     | TV         |    250 |
| 2001 | USA     | NULL       |   3000 |
| 2001 | NULL    | NULL       |   3010 |
| NULL | NULL    | NULL       |   7535 |
+------+---------+------------+--------+

解释

with rollup的工作原理就是在group by分组后,进行超级聚合。
它针对的是在group by后面出现的列,会把他们设置成null,表示不对这一列进行统计。
从最左边开始,找到不同的列值,把他们设置成null

在这里插入图片描述

例子2

SELECT year, country, product, SUM(profit) AS profit
FROM sales
GROUP BY country, year, product
在这里插入图片描述
超级聚合 with rollup后,
在这里插入图片描述

表以及数据

/*
Navicat MySQL Data TransferSource Server         : demo
Source Server Version : 50733
Source Host           : localhost:3306
Source Database       : demoTarget Server Type    : MYSQL
Target Server Version : 50733
File Encoding         : 65001Date: 2023-09-27 00:22:21
*/SET FOREIGN_KEY_CHECKS=0;-- ----------------------------
-- Table structure for `sales`
-- ----------------------------
DROP TABLE IF EXISTS `sales`;
CREATE TABLE `sales` (`country` varchar(20) DEFAULT NULL,`year` varchar(4) DEFAULT NULL,`product` varchar(20) DEFAULT NULL,`profit` int(4) DEFAULT NULL,`id` int(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of sales
-- ----------------------------
INSERT INTO `sales` VALUES ('Finland', '2000', 'Computer', '1500', '1');
INSERT INTO `sales` VALUES ('Finland', '2000', 'Phone', '100', '2');
INSERT INTO `sales` VALUES ('India', '2000', 'Calculator', '150', '3');
INSERT INTO `sales` VALUES ('India', '2000', 'Computer', '1200', '4');
INSERT INTO `sales` VALUES ('USA', '2000', 'Calculator', '75', '5');
INSERT INTO `sales` VALUES ('USA', '2000', 'Computer', '1500', '6');
INSERT INTO `sales` VALUES ('Finland', '2001', 'Phone', '10', '7');
INSERT INTO `sales` VALUES ('USA', '2001', 'Calculator', '50', '8');
INSERT INTO `sales` VALUES ('USA', '2001', 'Computer', '2700', '9');
INSERT INTO `sales` VALUES ('USA', '2001', 'TV', '250', '10');
http://www.lryc.cn/news/176618.html

相关文章:

  • 浅谈电动汽车充电桩设计与应用研究
  • tensorflow Windows安装说明
  • 【Leetcode热题】打卡 day11——20(更新至11)
  • linux使用操作[3]
  • 梦想让生活得以忍受-寄语机器视觉工程师
  • linux 设置打开文件数
  • MySQL基础篇-约束
  • 系统工程知识体系(SEBoK)
  • Spring DI (Dependency Injection)
  • Spring Boot : ORM 框架 JPA 与连接池 Hikari
  • Wireshark抓包分析ICMP协议
  • C++——安装环境、工具
  • 征稿啦!第 18 届「中国 Linux 内核开发者大会」重磅启动
  • JDBC8.0+
  • 聊聊常见的IO模型 BIO/NIO/AIO 、DIO、多路复用等IO模型
  • Linux- 网络编程初探
  • AVLoadingIndicatorView - 一个很好的Android加载动画集合
  • 我想设计一套游戏的奖励系统,有什么值得注意的?
  • 精通git,没用过git cherry-pick?
  • QT5|C++|通过创建子线程方式实现进度条更新
  • 基于mediasoup的webrtc server,性能压测时发现带宽利用率偏低(40%)
  • Ubuntu Redis开机自启动服务
  • Stm32_标准库_呼吸灯_按键控制
  • MySQL作业:索引、视图、存储、函数
  • 漫谈:C语言 C++ 所有编程语言 =和==的麻烦
  • 十五、异常(1)
  • Redis 哨兵模式搭建教程
  • 【C++】gnustl_static 与 c++_shared 的区别
  • 怎样选择第三方检测机构获取功能测试报告?
  • 【@PostConstruct、 @Autowired与构造函数的执行顺序】