现在的位置: 首页 > 综合 > 正文

Oracle学习<三>

2012年01月12日 ⁄ 综合 ⁄ 共 831字 ⁄ 字号 评论关闭

四、group function 组函数

  

  

    -| max函数,求最大值

    -| min函数,求最小值

    -| avg函数,求平均值

    -| count函数

    -| sum函数

    

    e.g:

      select to_char(avg(sal),'99999999,99') from emp;

      求sal中的平均值并精确到小数点后两位小数

      select round(avg(sal),2) from emp;
      结果:2073.21

      select count(*) from emp where deptno=10;
      select count(ename) from emp where deptno=10; count某个字段,如果这个字段不为空就算一个.
      select count(distinct deptno) from emp;

      select sum(sal) from emp;

 

 

 

五、 group  by 语句

    

    需求:现在想求,求每个部门的平均薪水.
      select avg(sal) from emp group by deptno;
      select deptno avg(sal) from emp group by deptno;

      select deptno,job,max(sal) from emp group by deptno,job;

      求薪水值最高的人的名字.
      select ename,max(sal) from emp;出错,因为max只有一个值,但等于max值的人可能好几个,不能匹配.
      应如下求:

  

      select ename from emp where sal=(select max(sal) from emp);

      Group by语句应注意,

      出现在select中的字段,如果没出现在组函数中,必须出现在Group by语句中.

【上篇】
【下篇】

抱歉!评论已关闭.