关于group by的使用示例
#2 empid number, --雇员ID 3 depid number, - -部门ID 4 area varchar(20), --区域 5 salenum number); --销售额
CREATE TABLE sales(
empid INT(10),
depid INT(10),
AREA VARCHAR(20),
salenm INT(20)
)DEFAULT CHARSET utf8;
INSERT INTO sales VALUES(1,1,'china',10);
INSERT INTO sales VALUES(2,1,'china',10);
INSERT INTO sales VALUES(3,1,'china',10);
INSERT INTO sales VALUES(4,1,'china',10);
INSERT INTO sales VALUES(5,1,'china',10);
INSERT INTO sales VALUES(6,1,'china',10);
INSERT INTO sales VALUES(7,1,'china',10);
INSERT INTO sales VALUES(8,2,'china',10);
INSERT INTO sales VALUES(9,2,'china',10);
INSERT INTO sales VALUES(10,3,'us',10);
INSERT INTO sales VALUES(11,3,'us',10);
SELECT * FROM sales;
-- 求出的是每个部分的销售额
SELECT depid,SUM(salenm) FROM sales GROUP BY depid;
-- 需求2,按部门统计销售额,并且只显示销售总额小于30的部门及销售额(使用having子句)
SELECT depid,SUM(salenm) FROM sales GROUP BY depid HAVING SUM(salenm) <30;