create table sales (
`year` int not null,
`country` varchar(20) not null,
`product` varchar(32) not null,
`profit` int
);
insert into sales values(2004, "china", "tnt1", 2001);
insert into sales values(2004, "china", "tnt2", 2002);
insert into sales values(2004, "china", "tnt3", 2003);
insert into sales values(2005, "china", "tnt1", 2004);
insert into sales values(2005, "china", "tnt2", 2005);
insert into sales values(2005, "china", "tnt3", 2006);
insert into sales values(2005, "china", "tnt1", 2007);
insert into sales values(2005, "china", "tnt2", 2008);
insert into sales values(2005, "china", "tnt3", 2009);
insert into sales values(2006, "china", "tnt1", 2010);
insert into sales values(2006, "china", "tnt2", 2011);
insert into sales values(2006, "china", "tnt3", 2012);
create table sales (
`year` int not null,
`country` varchar(20) not null,
`product` varchar(32) not null,
`profit` int
);
insert into sales values(2004, "china", "tnt1", 2001);
insert into sales values(2004, "china", "tnt2", 2002);
insert into sales values(2004, "china", "tnt3", 2003);
insert into sales values(2005, "china", "tnt1", 2004);
insert into sales values(2005, "china", "tnt2", 2005);
insert into sales values(2005, "china", "tnt3", 2006);
insert into sales values(2005, "china", "tnt1", 2007);
insert into sales values(2005, "china", "tnt2", 2008);
insert into sales values(2005, "china", "tnt3", 2009);
insert into sales values(2006, "china", "tnt1", 2010);
insert into sales values(2006, "china", "tnt2", 2011);
insert into sales values(2006, "china", "tnt3", 2012);
Jave代码
select year, country, product, sum(profit) from sales group by year, country, product;
+------+---------+---------+-------------+
| year | country | product | sum(profit) |
+------+---------+---------+-------------+
| 2004 | china | tnt1 | 2001 |
| 2004 | china | tnt2 | 2002 |
| 2004 | china | tnt3 | 2003 |
| 2005 | china | tnt1 | 4011 |
| 2005 | china | tnt2 | 4013 |
| 2005 | china | tnt3 | 4015 |
| 2006 | china | tnt1 | 2010 |
| 2006 | china | tnt2 | 2011 |
| 2006 | china | tnt3 | 2012 |
+------+---------+---------+-------------+
9 rows in set (0.00 sec)
select year, country, product, sum(profit) from sales group by year, country, product;
+------+---------+---------+-------------+
| year | country | product | sum(profit) |
+------+---------+---------+-------------+
| 2004 | china | tnt1 | 2001 |
| 2004 | china | tnt2 | 2002 |
| 2004 | china | tnt3 | 2003 |
| 2005 | china | tnt1 | 4011 |
| 2005 | china | tnt2 | 4013 |
| 2005 | china | tnt3 | 4015 |
| 2006 | china | tnt1 | 2010 |
| 2006 | china | tnt2 | 2011 |
| 2006 | china | tnt3 | 2012 |
+------+---------+---------+-------------+
9 rows in set (0.00 sec)
Jave代码
select year, country, product, sum(profit) from sales group by year, country, product with rollup;
+------+---------+---------+-------------+
| year | country | product | sum(profit) |
+------+---------+---------+-------------+
| 2004 | china | tnt1 | 2001 |
| 2004 | china | tnt2 | 2002 |
| 2004 | china | tnt3 | 2003 |
| 2004 | china | NULL | 6006 |
| 2004 | NULL | NULL | 6006 |
| 2005 | china | tnt1 | 4011 |
| 2005 | china | tnt2 | 4013 |
| 2005 | china | tnt3 | 4015 |
| 2005 | china | NULL | 12039 |
| 2005 | NULL | NULL | 12039 |
| 2006 | china | tnt1 | 2010 |
| 2006 | china | tnt2 | 2011 |
| 2006 | china | tnt3 | 2012 |
| 2006 | china | NULL | 6033 |
| 2006 | NULL | NULL | 6033 |
| NULL | NULL | NULL | 24078 |
+------+---------+---------+-------------+
16 rows in set (0.00 sec)
select year, country, product, sum(profit) from sales group by year, country, product with rollup;
+------+---------+---------+-------------+
| year | country | product | sum(profit) |
+------+---------+---------+-------------+
| 2004 | china | tnt1 | 2001 |
| 2004 | china | tnt2 | 2002 |
| 2004 | china | tnt3 | 2003 |
| 2004 | china | NULL | 6006 |
| 2004 | NULL | NULL | 6006 |
| 2005 | china | tnt1 | 4011 |
| 2005 | china | tnt2 | 4013 |
| 2005 | china | tnt3 | 4015 |
| 2005 | china | NULL | 12039 |
| 2005 | NULL | NULL | 12039 |
| 2006 | china | tnt1 | 2010 |
| 2006 | china | tnt2 | 2011 |
| 2006 | china | tnt3 | 2012 |
| 2006 | china | NULL | 6033 |
| 2006 | NULL | NULL | 6033 |
| NULL | NULL | NULL | 24078 |
+------+---------+---------+-------------+
16 rows in set (0.00 sec)
注意: Rollup 与 order by 相互排拆
分享到:
相关推荐
`CUBE`和`ROLLUP`是`WITH`子句中的两个特殊运算符,它们主要用于多维度数据分析和聚合操作,通常在`GROUP BY`语句中配合使用。 1. **CUBE运算符**: `CUBE`生成的结果集包含了所有可能的子集,即所选列中值的所有...
这里,`GROUP BY`后的列名用于确定分组,`HAVING`用于在分组后对结果进行过滤,`WITH ROLLUP`则用于添加额外的汇总行。常见的聚合函数有: - `COUNT()`:统计记录条数 - `SUM()`:计算字段的值的总和 - `AVG()`...
总的来说,`GROUP BY`、`DISTINCT`、`ORDER BY`、`GROUP_CONCAT`和`WITH ROLLUP`是MySQL中进行复杂查询和数据分析的关键工具。它们可以帮助你从大量数据中提取出有意义的信息,进行统计、排序、分组和聚合,进一步...
group by 属性名 [having 条件表达式][ with rollup] “属性名 ”指按照该字段值进行分组;“having 条件表达式 ”用来限制分组后的显示,满足条件的结果将被显示;with rollup 将会在所有记录的最后加上一条记录,...
3. GROUP BY with ROLLUP:GROUP BY语句配合WITH ROLLUP可以生成更详尽的分组统计信息,不仅包含各组的聚合结果,还包括整个数据集的汇总信息。 4. 大小写敏感性:MySQL默认情况下,数据库名、表名和列名的大小写...
例如,统计student表中每个民族的男女人数和总人数,SQL语句为`SELECT 民族, 性别, COUNT(*) AS 人数 FROM student GROUP BY 民族, 性别 WITH ROLLUP`。 5. `COUNT()`函数:用于计算某个字段的记录数,可以统计特定...
选择题部分涉及了MySQL的连接、版本查询、语句结束符设置、提示符设置、SQL脚本导入、SELECT语句用法、ENUM排序、LIMIT子句应用、聚合函数GROUP_CONCAT、GROUP BY WITH ROLLUP、SQL模式查看、无效值处理方式、...
例如,使用`RAND()`函数和`ORDER BY`子句来提取随机行,利用`GROUP BY`的`WITH ROLLUP`子句来执行更复杂的分组聚合操作。`RAND()`函数在MySQL中用于生成一个介于0到1之间的随机浮点数,可以用于选取数据表中的随机...
- 常用SQL技巧:分享了一些SQL使用中的技巧,如检索最大/最小值的行,使用rand()函数提取随机行,以及group by的with rollup子句等。 - 其他需注意的问题:例如数据库名、表名大小写问题,以及使用外键需要注意的...
userid WITH ROLLUP; 在上面的示例中,我们使用了 SUM 函数和 IF 语句来生成汇总行,并使用 GROUP BY 语句来分组。 MySQL 合并显示是将多个查询结果合并到一个查询结果的操作。例如,我们可以使用 UNION ALL 语句...
例如,SELECT department_id, AVG(salary) FROM employees GROUP BY department_id WITH ROLLUP; HAVING 子句 HAVING 子句用于过滤分组,例如,SELECT department_id, MAX(salary) FROM employees GROUP BY ...
WITH ROLLUP HAVING (GROUPING(行政) = 1) OR (GROUPING(起始时间) = 1) OR (GROUPING(仪表编码) = 0) AND (GROUPING(起始表码字) = 0) AND (GROUPING(终止表码字) = 0) AND (GROUPING(终止时间) = 0) AND ...
GROUP_CONCAT()函数用于将结果集中的每组数据连接成一个字符串,而GROUP BY with ROLLUP则能生成多级统计值。此外,试题还讨论了SQL模式,非严格模式下处理无效值的情况,严格模式下的处理策略,以及VARCHAR类型的...
- GROUP BY子句带WITH ROLLUP会产生多级统计值。 - 在非严格模式下,MySQL会尝试将无效值转化为合法值并产生警告,而不是直接拒绝。 这些知识点涵盖了MySQL数据库开发的基础操作和高级特性的理解和应用,对于学习者...
20. GROUP子句:GROUP子句带WITH ROLLUP的作用是产生多个级别的统计值。 21. 查看全局SQL模式:查看全局SQL模式的命令是select global.sql_mode。 22. 非严格模式:在非严格模式下处理无效值时,MySQL会基于列定义...
5. **SQL 功能扩展**:增加了窗口函数、GROUP BY with ROLLUP 和 CUBE 功能,以及用户定义变量的增强,提升了 SQL 编程的灵活性和功能性。 6. **复制改进**:改进了复制功能,包括GTID(全局事务标识符)的默认启用...
1. GROUP 子句带 WITH ROLLUP:产生多个级别的统计值。 查看全局 SQL 模式 1. 查看全局 SQL 模式的命令:select @@global.sql_mode 是查看全局 SQL 模式的命令。 严格模式 1. 严格模式:在严格模式下,拒绝超出...