Welcome 微信登录

首页 / 数据库 / MySQL / Oracle基础教程:聚集、分组、行转列

多行函数 聚集函数
执行顺序:
tName--where--group by --having--order by(select)where中不能出现当前子句中的别名,也不能用聚集(分组)函数聚集函数嵌套的时候,不能得到单个的列常用聚集函数
 是对一组或一批数据进行综合操作后返回一个结果
 count 行总数--处理空值,空值也算进去了
  count(distinct column)
  count(all column) all是默认参数,可以不写
 avg 平均数--不处理空值
 sum 列值的和--不处理空值
 max 最大值
 min 最小值count([{distinct|all} "列名"|*) 为列值时空不在统计之内
  为*时包含空行和重复行
idle> select count(comm) from emp;COUNT(COMM)
-----------
   4idle> select count(ename) from emp;COUNT(ENAME)
------------
   14idle> select count(*) from emp;  COUNT(*)
----------
 14idle>  
idle> select count(deptno) from emp;COUNT(DEPTNO)
-------------
    14idle> select count(distinct deptno) from emp;COUNT(DISTINCTDEPTNO)
---------------------
      3idle> select count(all deptno) from emp;COUNT(ALLDEPTNO)
----------------
       14idle>  
idle> select avg(sal),sum(sal),max(sal),min(sal),count(sal) from emp;  AVG(SAL)   SUM(SAL) MAX(SAL)   MIN(SAL) COUNT(SAL)
---------- ---------- ---------- ---------- ----------
2073.21429 29025     5000 800     14idle>
上面执行的聚集函数都是对所有记录统计
如果想分组统计(比如统计部门的平均值)需要使用group by 为了限制分组统计的结果需要使用having过滤
GROUP BY 分组统计  9I要排序 10G不排序
相同部门相同职位的平均工资
select deptno,job,avg(sal) from emp group by deptno,job;求出每个部门的平均工资idle> select deptno,avg(sal) from emp group by deptno;    DEPTNO   AVG(SAL)
---------- ----------
 30 1566.66667
 20  2175
 10 2916.66667idle>
分组再排序
idle> select deptno,avg(sal) from emp group by deptno order by deptno ;    DEPTNO   AVG(SAL)
---------- ----------
 10 2916.66667
 20  2175
 30 1566.66667idle>
分组修饰列可以是未选择的列
idle> select avg(sal) from emp group by deptno order by deptno ;  AVG(SAL)
----------
2916.66667
      2175
1566.66667idle>上面执行的分组函数都是对所有记录统计,如果想分组统计(比如统计部门的平均值)需要使用group by 为了限制分组统计的结果需要使用having过滤
GROUP BY 分组统计  9I要排序 10G不排序求出没个部门的平均工资idle> select deptno,avg(sal) from emp group by deptno;    DEPTNO   AVG(SAL)
---------- ----------
 30 1566.66667
 20  2175
 10 2916.66667idle>
分组再排序
idle> select deptno,avg(sal) from emp group by deptno order by deptno ;    DEPTNO   AVG(SAL)
---------- ----------
 10 2916.66667
 20  2175
 30 1566.66667idle>
分组修饰列可以是未选择的列
idle> select avg(sal) from emp group by deptno order by deptno ;  AVG(SAL)
----------
2916.66667
      2175
1566.66667idle>如果在查询中使用了分组函数,任何不在分组函数中的列或表达式必须在group by子句中
因为分组函数是返回一行 而其他列显示多行 显示结果矛盾.
idle> select avg(sal) from emp ;  AVG(SAL)
----------
2073.21429idle> select deptno,avg(sal) from emp;
select deptno,avg(sal) from emp
       *
ERROR at line 1:
ORA-00937: not a single-group group function
idle> select deptno,avg(sal) from emp group by deptno ;    DEPTNO   AVG(SAL)
---------- ----------
 30 1566.66667
 20  2175
 10 2916.66667idle> select deptno,avg(sal) from emp group by deptno order by job;
select deptno,avg(sal) from emp group by deptno order by job
                                                         *
ERROR at line 1:
ORA-00979: not a GROUP BY expression
idle> group by多条件分组
SCOTT@ora10g> select deptno,job,avg(sal),max(sal) from emp group by deptno,job order by 1;    DEPTNO JOB        AVG(SAL)   MAX(SAL)
---------- --------- ---------- ----------
 10 CLERK    1300       1300
 10 MANAGER    2450       2450
 10 PRESIDENT    5000       5000
 20 ANALYST    3000       3000
 20 CLERK     950       1100
 20 MANAGER    2975       2975
 30 CLERK     950        950
 30 MANAGER    2850       2850
 30 SALESMAN    1400       16009 rows selected.SCOTT@ora10g>
  • 1
  • 2
  • 下一页
Oracle ORA-0650 快照过旧实验Oracle基础教程:RHEL5U5安装Oracle 10g相关资讯      oracle数据库教程 
  • Oracle raw数据类型介绍  (01/29/2013 10:05:53)
  • 监听器注册与ORA-12514 错误分析  (11/13/2012 14:30:08)
  • Oracle SQL的cursor理解  (11/13/2012 14:16:17)
  • Oracle 如何强制刷新Buffer Cache  (01/29/2013 10:02:46)
  • dblink致Oracle库的SCN变成两库的  (11/13/2012 14:24:41)
  • Linux操作系统下完全删除Oracle数  (11/13/2012 08:25:52)
本文评论 查看全部评论 (0)
表情: 姓名: 字数