自学内容网 自学内容网

sql group by 多个字段例子

有表如下;

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

 

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

 

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;

 


原文地址:https://blog.csdn.net/bcbobo21cn/article/details/144780188

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!