zoukankan      html  css  js  c++  java
  • 组函数及分组统计


    分组函数


    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 emp
     GROUP BY deptno
    

    Oracle会提示第 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 BYdeptno<span style="font-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_sal
      from dept d,
       (select deptno,avg(sal) avg_sal
          from emp
           group by deptno having avg(sal)=
              (select max(avg(sal)) from emp group by deptno) 
       ) t
    where t.deptno=d.deptno;








  • 相关阅读:
    SQL盲注工具BBQSQL
    嗅探X-Windows服务按键工具xspy
    多协议底层攻击工具Yesinia
    LLMNR欺骗工具Responder
    Arduino可穿戴教程保存源文件与打开已经存在的源文件
    GRDB使用SQLite的WAL模式
    CString之GetBuffer与ReleaseBuffer
    VC++ 模块与资源分离
    KV6.60 SP1
    Html之head部分详解
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5278895.html
Copyright © 2011-2022 走看看