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;
相关推荐
萧鼎13 分钟前
深度探索 Py2neo:用 Python 玩转图数据库 Neo4j
数据库·python·neo4j
m0_653031361 小时前
腾讯云认证考试报名 - TDSQL数据库交付运维专家(TCCE MySQL版)
运维·数据库·腾讯云
power 雀儿1 小时前
集群聊天服务器---MySQL数据库的建立
服务器·数据库·mysql
骑着王八撵玉兔3 小时前
【性能优化与架构调优(二)】高性能数据库设计与优化
数据库·性能优化·架构
想要入门的程序猿4 小时前
Qt写入excel
数据库·qt·excel
Q_970956394 小时前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
Wyc724094 小时前
Maven
java·数据库·maven
程序猿小D4 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
羊小猪~~4 小时前
数据库学习笔记(十七)--触发器的使用
数据库·人工智能·后端·sql·深度学习·mysql·考研
背太阳的牧羊人5 小时前
Neo4j 的向量搜索(Neo4jVector)和常见的向量数据库(比如 Milvus、Qdrant)之间的区别与联系
数据库·neo4j·milvus