group by 的过滤
查出平均工资大于2000的部门
idle> select deptno,avg(sal) avg from emp group by deptno where avg >2000;
select deptno,avg(sal) avg from emp group by deptno where avg >2000
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
group by后不能再接where子句过滤 where过滤只能加到group by前端 这样又不能满足要求
对分组后的过滤要使用having
idle> select deptno,avg(sal) avg from emp group by deptno having avg(sal) >2000;
DEPTNO AVG
---------- ----------
20 2175
10 2916.66667
idle>
where与having一同使用
idle> select deptno,avg(sal) avg from emp where deptno != 10 group by deptno having avg(sal) >2000;
DEPTNO AVG
---------- ----------
20 2175
idle>
idle>
分组函数的嵌套
不嵌套效果
SCOTT@ora10g> select avg(sal),avg(sal) from emp where ename != 'KING' group by job;
AVG(SAL) AVG(SAL)
---------- ----------
1037.5 1037.5
1400 1400
2758.33333 2758.33333
3000 3000
SCOTT@ora10g>
求每个职位的最高平均工资和最低平均工资
idle> select min(avg(sal)),max(avg(sal)) from emp where ename != 'KING' group by job;
MIN(AVG(SAL)) MAX(AVG(SAL))
------------- -------------
1037.5 3000
idle>
求平均奖金 avg(comm) 即可 因为除了count(*)以外 其他分组函数都不处理空
idle> select avg(comm) from emp;
AVG(COMM)
----------
550
idle>
想求出所有员工的平均 需要使用nvl
idle> select avg(nvl(comm,0)) from emp;
AVG(NVL(COMM,0))
----------------
157.142857
idle>
分组函数的注意事项:
1.分组函数只能出现在选择列,order by,having子句中
2.分组函数会忽略NULL 除了count(*)
3.分组函数中可以使用ALL或distinct;ALL是默认值,统计所有.加上distinct则只统计不同
4.如果选择的列里有普通列,表达式和分组列 那么普通列和表达式都必须出现在group by中
如下操作:得到t5表中有部分行是重复的,找出重复的行
SQL> create table t5 as select * from emp;
Table created.
SQL> insert into t5 select * from emp where deptno=20;
5 rows created.
SQL> commit;
Commit complete.
SQL>select count(empno) from t5 group by empno having count(*)>1;
行转列
create table t4(id int,name varchar2(10),subject varchar2(20),grade number);
insert into t4 values(1,'ZORRO','语文',70);
insert into t4 values(2,'ZORRO','数学',80);
insert into t4 values(3,'ZORRO','英语',75);
insert into t4 values(4,'SEKER','语文',65);
insert into t4 values(5,'SEKER','数学',75);
insert into t4 values(6,'SEKER','英语',60);
insert into t4 values(7,'BLUES','语文',60);
insert into t4 values(8,'BLUES','数学',90);
insert into t4 values(9,'PG','数学',80);
insert into t4 values(10,'PG','英语',90);
commit;
SQL> select * from t4;
ID NAME SUBOBJECT GRADE
---------- ---------- -------------------- ----------
1 ZORRO 语文 70
2 ZORRO 数学 80
3 ZORRO 英语 75
4 SEKER 语文 65
5 SEKER 数学 75
6 SEKER 英语 60
7 BLUES 语文 60
8 BLUES 数学 90
9 PG 数学 80
10 PG 英语 90
10 rows selected.
SQL> select name,max(decode(subject,'yuwen',grade,0) from t4
group by name;
----------------------------
| name | 语文 | 数学| 英语|
----------------------------
| zorro | 70 | 80 | 75 |
----------------------------
| seker | 65 | 75 | 60 |
----------------------------
| blues | 60 | 90 | 0 |
----------------------------
| PG | 0 | 80 | 90 |
----------------------------
SQL> select name,sum(case when SUBJECT='语文' then GRADE else 0 end) "语文",sum(case when SUBOBJECT='数学' then GRADE else 0 end) "数学",sum(case when SUBJECT='英语' then GRADE else 0 end) "英语" from t4 group by name;
NAME 语文 数学 英语
---------- ---------- ---------- ----------
SEKER 65 75 60
BLUES 60 90 0
PG 0 80 90
ZORRO 70 80 75
SQL>
