oracle中常见的分组函数


1、求平均值:AVG()

      求和:sum()

      求最大值:  MAX()

      最小值: min()

      行列互换 :  wm_concat()

2、分组函数会自动过滤空值,需要格外注意

3、where后不可以加 分组函数;having可以

     如果不存在分组函数,where和having可以互换使用

     where是先过滤后分组,查询效率快

     having是先分组在过滤,查询效率慢

需注意在使用group by 时 where和having的位置:

select   t.deptno, sum(t.money)/count(*),sum(t.money)/count(t.money),avg(t.money),avg(nvl(t.money,0)) from emp t
where t.deptno ='1'
 group by t.deptno;


select   t.deptno, sum(t.money)/count(*),sum(t.money)/count(t.money),avg(t.money),avg(nvl(t.money,0)) from emp t
 group by t.deptno  having t.deptno ='1'

4、关于wm_concat()行列互换

如果字符拼接长度过长,会显示clob

可以使用to_char(),超过4000则报错;也可以使用dbms_lob.substr(wm_concat( t.name),len)截取指定长度的字符串,同样超过4000会报错

select   t.deptno,dbms_lob.substr(wm_concat( t.name),20)  from emp t
 group by t.deptno;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM