sql group by 多个字段例子

有表如下;

获取某年份、某地区、某产品的销售总额,

或者根据需要把字段顺序换一下;

sql 复制代码
insert into sales (product, year, region, amount) values ('飞机','2000','东部',5);
insert into sales (product, year, region, amount) values ('飞机','2001','中部',11);
insert into sales (product, year, region, amount) values ('飞船','2000','西部',6);
insert into sales (product, year, region, amount) values ('飞艇','2000','东部',7);
insert into sales (product, year, region, amount) values ('飞机','2000','北部',5);
insert into sales (product, year, region, amount) values ('飞机','2001','南部',13);
insert into sales (product, year, region, amount) values ('飞船','2001','东部',9);
insert into sales (product, year, region, amount) values ('飞船','2000','北部',17);
insert into sales (product, year, region, amount) values ('飞艇','2000','东部',21);
insert into sales (product, year, region, amount) values ('飞船','2000','东部',23);
insert into sales (product, year, region, amount) values ('飞机','2000','东部',23);
insert into sales (product, year, region, amount) values ('飞船','2001','东部',23);

select * from sales;

SELECT year, region, product, SUM(amount) AS 销售总额
FROM sales
GROUP BY year, region, product;

SELECT product,year, region,  SUM(amount) AS 销售总额
FROM sales
GROUP BY product,year, region;
相关推荐
席万里27 分钟前
MySQL有哪些锁?
数据库·mysql
keep.ac1 小时前
MySQL为什么选择使用B+树作为索引结构?
数据库·b树·mysql
轻风细雨1 小时前
hive-sql 计算每年在校生人数
hive·hadoop·sql
栀寒老醑1 小时前
Zabbix企业级分布式监控系统
服务器·网络·数据库·redis·mysql·云计算·zabbix
hking1112 小时前
BUUCTF sqli-labs 1
sql·web安全
dami_king3 小时前
SQL如何添加数据?|SQL添加数据示例
数据库·sql·db
儒道易行3 小时前
【SQLi_Labs】Basic Challenges
数据库·web安全·网络安全
Clarkbrave3 小时前
PHP框架+gatewayworker实现在线1对1聊天--mysql数据库(3)
数据库·mysql·php
程序员志哥3 小时前
并发编程系列(七) -本地线程ThreadLocal介绍
java·数据库
乐维_lwops3 小时前
万里数据库GreatSQL监控解析
数据库·oracle·数据库监控·运维监控