您好,欢迎访问一九零五行业门户网

ORACLE单行函数与多行函数之七:多行函数之分组函数示例

多行函数: 从一组记录中返回一条记录,可出现在select列表、order by和having子句中 通常都可用distinct过滤掉重复的记录,默认或用all来表示取全部记录 无论是否过滤重复记录, null在聚合函数中总是不被计算,被忽略。 主要函数: –count –min和max –a
多行函数:
从一组记录中返回一条记录,可出现在select列表、order by和having子句中
通常都可用distinct过滤掉重复的记录,默认或用all来表示取全部记录
无论是否过滤重复记录,null在聚合函数中总是不被计算,被忽略。
主要函数:
–count
–min和max
–avg和sum---只可用于数值型
1.count统计行数函数,及where、group by、having的应用。
null值被过滤掉,不计入统计。
bys@bys1>select count(*), count(comm) from emp;
  count(*) count(comm)
---------- -----------
        14           4
按部门号分组,统计每个部门的员工数。
bys@bys1>select deptno,count(empno) from emp group by deptno;
    deptno count(empno)
---------- ------------
        30            6
        20            5
统计每个部门的员工数,不统计 empno=7788的员工
bys@bys1>select deptno,count(empno) from emp where empno!=7788 group by deptno;
    deptno count(empno)
---------- ------------
        30            6
        20            4
        10            3
统计每个部门的员工数,不统计 empno=7788的员工,统计完后不显示deptno为10的部门。
bys@bys1>select deptno,count(empno) from emp where empno!=7788 group by deptno having deptno!=10;
deptno count(empno)
---------- ------------
        30            6
        20            4
2.sum求和,只可用于数值型求所有员工工资和
bys@bys1>select sum(sal) from emp;
sum(sal)
----------
     29025
统计每个部门的工资总和,不统计empno=7788的员工工资,同时统计完后不显示deptno为10的部门。
bys@bys1>select deptno,sum(sal) from emp where empno!=7788 group by deptno having deptno!=10;
deptno   sum(sal)
---------- ----------
        30       9400
        20       7875
3.avg求平均数,只可用于数值型bys@bys1>select avg(sal) from emp;
  avg(sal)
----------
2073.21429
bys@bys1>select deptno,avg(sal) from emp where empno!=7788 group by deptno having deptno!=10;
    deptno   avg(sal)
---------- ----------
        30 1566.66667
        20    1968.75
4.max和min:求最大和最小值
bys@bys1>select deptno,max(sal),min(sal) from emp where empno!=7788 group by deptno having deptno!=10;
    deptno   max(sal)   min(sal)
---------- ---------- ----------
        30       2850        950
        20       3000        800
bys@bys1>select max(sal),min(sal) from emp;
  max(sal)   min(sal)
---------- ----------
      5000        800
5.group by分组,可以指定一列或多列。多列时,先按第一列分组,然后在分组中,如果有符合第二列的,再进行分组。关于group by及rollup和cube参数,更详细的在:http://blog.csdn.net/q947817003/article/details/13904763
bys@bys1>select deptno,job,max(sal),min(sal) from emp where empno!=7788 group by deptno,job having deptno!=10;
    deptno job         max(sal)   min(sal)
---------- --------- ---------- ----------
        20 clerk           1100        800
        30 salesman        1600       1250
        20 manager         2975       2975
        30 clerk            950        950
        30 manager         2850       2850
        20 analyst         3000       3000
6.having,用于在分组后的结果中进行过滤。可以在having中使用分组函数。在where中不行。因为where执行时,尚未分组,而having时,已经分组完成。having与where比较说明更详细在:http://blog.csdn.net/q947817003/article/details/13622215
求部门平均工资大于2000的部门编号。
bys@bys1>select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
    deptno   avg(sal)
---------- ----------
        20       2175
        10 2916.66667
其它类似信息

推荐信息