Oraclel:多行函数

注意:多行函数不统计null的值。
1、count()函数:统计总记录数;
     注意:若使用count(*),若表字段较少的情况下,可以用*;若表字段多的情况下,扫描时间多,效率低下。因此在项目中提                   倡使用某一个非null的唯一的字段,通常是主键。
      例1:统计总人数:select count(*) from emp;
      例2:统计公司有多少个不重复的部门:select count(distinct deptno) from emp;
2、sum()求和
3、avg()求平均值
4、round(a,n):四舍五入,保留小数点后n位。
5、max()最大,min()最小。一般用在数值型,也可以用在日期型,若是日期型,max是年近者。
6、having:分组后再作的操作:
    例1:查询部门平均工资大于2000元的部门:
          select deptno trunc(avg(sal),0)
          from emp
          group by deptno
          having trunc(avg(sal),0);
     例2按部门平均工资降序排序:
          select deptno trunc(avg(sal),0)
          from emp
          group by deptno
          having trunc(avg(sal),0)
          order by  trunc(avg(sal),0) desc;
    注意:order by必须放在最后。
7、 注意:多行函数可以嵌套,先执行里面的,再执行外面的,理论上可以无限嵌套,但是嵌套越多,会影响效率。
     例:显示部门平均工资的最大值:
         select max(avg(sal))
         from emp
         group by deptno;

猜你喜欢

转载自blog.csdn.net/weixin_41113108/article/details/80279677