• 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.2)from 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  是对筛选之后的分组进行排序
  • 相关阅读:
    solidworks 学习 (二)洗手液瓶
    solidworks 学习 (一)螺丝刀
    tensorflow 2.0 学习(三)MNIST训练
    tensorflow 2.0 学习(二)线性回归问题
    tensorflow 2.0 学习(一)准备
    sscanf linux-c从一个字符串中读进与指定格式相符的数据
    Linux-c glib库hash表GHashTable介绍
    Linux-c给线程取名字
    linux-c getopt()参数处理函数
    golang Linux下编译环境搭建
  • 原文地址:https://www.cnblogs.com/longmo666/p/13552946.html
Copyright © 2020-2023  润新知