oracle聚合函数,条件判断

--查询的基本语法 select * from emp; select * from emp where empno = 7369 order by sal desc --oracle常用函数 --Oracle提供了一些可以执行特定操作的方法 --字符函数:对字符串类型的字段进行处理 select ascii('a') from dual; --拼接字符串 select concat('hello', 'world') from dual; select concat('h', 'w') from dual; --获取字符串的长度 select length(ename) from emp; --从字符串中查找子字符串,返回位置,下标从1开始,若找不到,返回0 select instr('hello world', 'or') from dual; --转小写 select ename, lower(job) from emp; --转大写 select upper('hello') from dual; --去空格 select ltrim(' hello') from dual; --去右空格 select rtrim('hello ') from dual; --去横杠 select ltrim('--hello' '-') from dual; --去掉两边的空格 select trim(' hello ') from dual; --去两边的横杠 select trim('-' from '--hello--') from dual; --字符串的替换 select replace'abcd', 'c', 'z'from dual; --截取子字符串 select substr('abcde ', 2) from dual; --2代表起始位置,3代表截取的字符长度 select substr('ABCDE', 2, 3) from dual; --数字函数 --求绝对值 select abs(-3) from dual; --反余弦 select acos(1) from dual; --余弦 select cos(1) from dual; --大于或等于这个参数的最小值 select ceil(5.4) from dual; --小于或者等于这个参数的最大数 select floor( - 1.2from dual; --求对数 select log(2, 8) from dual; --求余数 select mod(8, 3) from dual; --求幂数 select power(2, 3) from dual; --四舍五入 select round(45.67) from dual; select round(100.579, 2) from dual; --截断方法 select trunc(45.97) from dual; select trunc(100.579) from dual; --平方根 select sqrt(25) from dual; --日期函数 --加上指定月数,返回新的日期 select hiredate, add_months(hiredate, 3) from emp; --返回指定日期所在月的最后一天 select last_day(hiredate) from emp; --返回两个日期之间相隔的月数 select months_between(sysdate, hiredate) from emp; --转换函数***** select to_date('1999-09-09', 'yyyy-mm-dd') from dual; select to_char(sysdate, 'yyyy-mm-dd') from dual; select substr(to_char(hiredate, 'yyyy-mm-dd'), 1, 4) from emp; select to_number('123.45') from dual; --为空值赋值 select comm + 500 from emp; select nvl(comm, 0) from emp; --条件判断 select decode(job, 'CLERK','业务员' 'SALESMAN','销售' 'MANAGER','经理') from emp; select case job when 'CLERK' then '业务员' when 'SALESMAN' then '销售' end from emp; --以上函数称为单行函数 --输入一个常量返回一个结果,或输入一个字段名会针对每一条记录返回一个结果 --聚合函数*************** --平均工资 select avg(sal) from emp; --工资和 select sum(sal) from emp; --员工数 select count(empno) from emp; select count(*) from emp; --最值 select max(sal) from emp; select min(sal) from emp; --使用了聚合函数后,不要再查询其他字段 select ename from emp where sal = (select max(sal) from emp); select ename from emp where sal > (select avg(sal) from emp); --分组操作********************** select deptno, count(empno), avg(sal), sum(sal) from emp group by deptno; select job, avg(sal) from emp group by job; --分组后,select只能用来查询分组的字段和聚合函数 --平均工资大于2000的部门编号 select deptno, avg(sal) avg_sal from emp group by deptno having avg(sal) > 2000; --各个部门下CLERK的平均工资 select deptno, avg(sal) from emp where job = 'CLERK' group by deptno having avg(sal) > 1000; --执行顺序 -->where-->group by --->having---> order by --where 分组之前过滤,对表中全部数据进行筛选 --group by 对筛选之后的数据进行分组 --having 是筛选出符合条件的分组 --order by 是对筛选之后的分组进行排序

 


__EOF__

本文作者龙陌
本文链接https://www.cnblogs.com/longmo666/p/13552946.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   龙陌  阅读(832)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示