拼团购物网站怎么做,php 网站开发360,wordpress 曹鹏,惠东网站开发分组函数 SQL中经常使用的分组函数 Count(): 计数 Max()#xff1a;求最大值 Min()#xff1a;求最小值 Avg()#xff1a;求平均值 Sum()#xff1a;求和 -- 统计emp表中的人数
select count(*) from emp; -- 统计获得奖金的人数
select count(comm) from emp;-- 求全部雇… 分组函数 SQL中经常使用的分组函数 Count(): 计数 Max()求最大值 Min()求最小值 Avg()求平均值 Sum()求和 -- 统计emp表中的人数
select count(*) from emp; -- 统计获得奖金的人数
select count(comm) from emp;-- 求全部雇员的最低工资
select min(sal) from emp;-- 求全部雇员的最高工资
select max(sal) from emp;-- 求部门编号为20的雇员的平均工资和总工资
select avg(sal),sum(sal) from emp where deptno 20;分组统计查询 语法格式 SELECT {DISTINCT}*|查询列1 别名1查询列2 别名2…… FORM 表名称1 别名1,表名称2 别名2,…… {WHERE 条件表达式} {GROUP BY 分组条件} {ORDERBY 排序字段 ASC|DESC,排序字段 ASC|DESC,……} -- 统计出每一个部门的人数
select deptno,count(empno) from emp group by deptno;-- 求出每一个部门的平均工资
select deptno, avg(sal) from emp group by deptno; 统计每一个部门的最高工资以及获得最高工资的雇员姓名 假设写成 SELECT ename,max(sal)FROM empGROUP BY deptnoOracle会提示第 1 行出现错误: ORA-00979: 不是 GROUP BY 表达式 以上代码在运行过程中出现错误是由于: 1. 假设程序中使用了分组函数。则在下面两种情况下能够正常查询结果: 程序中存在了GROUP BY,并指定了分组条件。这样能够将分组条件一起查询出来 假设不使用GROUP BY,则仅仅能单独地使用分组函数 2使用分组函数时查询结果列不能出现分组函数和分组条件之外的字段 综上所述我们在进行分组统计查询时有遵循这样一条规律: 出如今字段列表中的字段。假设没有出如今组函数中。就必然出如今GROUP BY 语句的后面 -- 统计出每一个部门的最高工资。及最高工资的雇员姓名
select deptno, ename,sal from emp where sal in(select max(sal) from emp group by deptno); -- 查询出每一个部门的部门名称。及每一个部门的雇员人数
select d.dname, count(e.empno)
from emp e, dept d
where e.deptno d.deptno
group by d.dname 求出平均工资大于2000的部门编号和平均工资 刚開始学习的人非常easy错误地写成将工资大于2000的条件写在where的后面 SELECT deptno,avg(sal) FROM emp WHERE avg(sal)2000 GROUP BYdeptnospan stylefont-family:SimSun;/span 系统出现例如以下错误提示: ORA-00934: 此处不同意使用分组函数 -- 求出平均工资大于2000的部门编号和平均工资
select e.deptno, avg(sal)
from emp e, dept d
where e.deptno d.deptno
having avg(sal) 2000
group by e.deptno; 规则WHERE 仅仅能对单条记录限制过滤having是对分组进行过滤 分组函数仅仅能在分组中使用。不能在WHERE语句之中出现。假设要指定分组条件则仅仅能通过另外一种条件的指令HAVING -- 显示非销售人员工作名称以及从事同一工作雇员的月工资总和而且要满足从事同一工作的雇员的月工资合计大于$5000,输出结果按月工资合计升序排列
select e.job, sum(e.sal) sum_sal
from emp e
where e.job SALESMAN
group by e.job
having sum(e.sal) 5000
order by sum_sal;分组的简单原则 仅仅要一列上存在反复内容才有可能考虑到用分组查询 注意 分组函数能够嵌套使用可是在组函数嵌套使用的时候不能再出现分组条件的列名 例求平均工资最高的部门编号、部门名称、部门平均工资 第一步 select deptno, avg(sal) from emp group by deptno; 第二步 select deptno, max(avg(sal)) from emp group by deptno;ORA-00937: 不是单组分组函数 第三步去掉查找结果中的deptno列 select max(avg(sal)) from emp group by deptno; 逐步完毕后 select d.deptno, d.dname, t.avg_salfrom dept d,(select deptno,avg(sal) avg_salfrom empgroup by deptno having avg(sal)(select max(avg(sal)) from emp group by deptno) ) t
where t.deptnod.deptno; 转载于:https://www.cnblogs.com/yxwkf/p/5278895.html