zoukankan      html  css  js  c++  java
  • Oracle数据库学习笔记4

    --trunc(数字,小数点的位数)截取
    --round(数字,四舍五入要保留的位数)
    select ename,sal*0.1234567 s1,
    round(sal*0.1234567,2) s2,
    round(sal*0.1234567) s3
    from emp;
    --sysdate:系统时间,精确到秒
    --systimestamp:系统时间,精确到毫秒
    --计算员工入职多少个月
    select * from emp;
    --months_between
    select ename,hiredate,month_between(sysdate,hiredate) months
    from emp;
    --add_months
    --12个月之前的时间点
    select add_months(sysdate,12) from dual;
    --last day
    --计算本月的最后一天
    select last_day(sysdate) from dual;
    --转换函数
    --to_char(日期数据,日期格式):把日期转换为字符数据
    --yyy--mm--dd hh:mi:ss
    select ename,to_char(hiredate,'yyyy-mm-dd hh24:mi:ss') from emp;
    --统计那些员工在本月过生日
    select empno,ename,hiredate
    from emp
    where to_char(hiredate,'mm')
          =to_char(sysdate,'mm');
    --查询那些员工是1981年入职的
    select empno,ename,hiredate
    from emp
    where to_char(hiredate,'yyyy')=1981;
    --to_date():把字符转换成日期格式
    select * from student;
    insert into student(stu_id,stu_name,stu_birth)
    values(4,'张三',
    to_date('2011-1-1','yyyy-mm-dd'));
    --coalesce(参数列表):返回参数列表的第一个非空值
    --计算员工的年终奖
    --如果comm不是null,年终奖发comm
    --如果comm是null,年终奖底薪的50%
    --如果sal和comm都为null,发100的安慰奖
    select empno,ename,sal,comm,
           coalesce (comm,sal*0.5,100)
           from emp;
          
    --case语句
    --根据员工的工作岗位
    --job=SALES -->10%
    --job=MANAGER --->5%
    --job=CLERK -->2%
    select ename,sal,job,
    case job when 'SALES' then sal*1.1
             when 'MANAGER' then sal*1.05
             when 'CLERK' then sal*1.02
             else sal--相当于java中case的default语句
    end new_sal
    --新列
    from emp;
    --decode函数
    --decode(判断条件,匹配1,值1,匹配2,值2......default) 新列名
    select ename,sal,job,
    decode(job,'SALES',sal*1.1,
                'MANAGER',sal*1.05,
                'CLERK',sal*1.02,
                sal) new_sal
    from emp;
    --length()
    select ename,length(ename) from emp;
    --lpad rpad
    select ename,lpad(ename,10,'*') from emp;
    --ASCLL码转换
    select chr(97) from emp;
    --排序 order by 排序字段 排序方式 (升序,降序)
    --升序 asc (默认) 降序:desc
    --薪水由低到高排序
    select ename,sal from emp
    order by sal --asc;
    select ename,sal from emp
    order by sal desc;
    --查出来的表不是数据库里的表,只是一个临时的结果集
    --按入职时间排序,入职时间越早的排在前面
    select ename,hiredate
    from emp
    order by hiredate;--排序语句放在查询语句的最后
    --组函数(聚合函数:count,sum,max,min,avg)
    --空值处理检验
    insert into emp(empno,ename,deptno)
    values(9999,'JACK1',20);
    select count(*) from emp;
    select sum(sal) from emp;
    --一定要处理空值,不然结果不变
    select avg(nvl(sal,0)) from emp;
    --求员工的人数总和,薪水总和,最高薪水,最低薪水,平均薪水
    select count(*),sum(sal),max(sal),min(sal),avg(nvl(sal,0)) from emp;
    select sum(sal) from emp;
    select max(sal) from emp;
    select min(sal) from emp;
    select avg(nvl(sal,0)) from emp;
    --求每个部门的最高和最低薪水(分组)
    --group by 分组的字段
    --没有用聚合函数的一定要跟在group by后面
    select deptno,max(sal) 最高薪水,
           min(sal) 最低薪水
           from emp
           group by deptno;
          
    --按职位分组,求每个职位的最高和最低薪水
    select job,max(sal),min(sal)
    from emp
    group by job;
    --筛选 having字句:用于对分组后的数据进行过滤
    --平均薪水大于2500的部门数据,没有部门的不算
    select deptno,avg(nvl(sal,0)) avg_s
    from emp
    where deptno is not null
    group by depno
    having avg(nvl(sal,0)) > 2500;
    --哪些职位的人数超过两个
    select job,count(*) emp_num
    from emp
    where job is not null
    group by job
    having count(*) > 2
    order by emp_num;
  • 相关阅读:
    Leetcode Valid Sudoku
    Leetcode Surrounded Regions
    LeetCode Sqrt
    LeetCode POW
    LeetCode Next Permutation
    ACK-Ackermann, 阿克曼函数
    再不懂时序就 OUT 啦!,DBengine 排名第一时序数据库,阿里云数据库 InfluxDB 正式商业化!
    阿里云提供全托管 ZooKeeper
    性能压测中的SLA,你知道吗?
    第一个入驻阿里云自营心选商城,如今它已经是营收过亿的SaaS独角兽
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7172330.html
Copyright © 2011-2022 走看看