您的位置:首页 > 数据库 > Oracle

day02 笔记(下)数据库Oracle

2020-03-29 13:07 1086 查看

4.单行函数

——当前时间
select distinct sysdate from emp;
select sysdate from dual;
select current_date from dual;
——加减日期 两天以后十几号
select sysdate+2 from dual;
——add_months(查询日期, 增加月数) 查询所有员工的试用期到期 3个月试用期
select ename, empno, hiredate 入职日期, add_months(hiredate,3) 转正日期 from emp;
——months_between(目标日期,查询日期) ——查询当前月的最后一天
select sysdate last_day(sysdate) from dual;

4.1日期与字符转换:

——to_date(‘日期字符串’,‘yyyy-mm-dd hh24:mi:ss’) 将字符串转化为日期
select to_date('2018-9-5 16:18:25','yyyy-mm-dd hh24:mi:ss') from dual;
——to_char(‘日期’,‘yyyy"年"mm"月"dd hh24:mi:ss’)
select to_char(sysdate,'yyyy"年"mm"月"dd hh24:mi:ss') from dual;

日期与字符串的相互转换重要
练习:查询82年入职员工的信息

select ename,hiredate,to_char(hiredate,'yyyy') from emp where to_char(hiredate,'yyyy') = '1982';

4.2判定函数 decode(判定字段,值1,结果1,值2,结果2…,默认值)

根据字段的值判定函数最终的结果,如果以上值判定都不相等,给默认值

select deptno,dname,decode(deptno,10,'十',20,'二十',30,'三十','四十') from dept;

–case when then else end

select ename,
sal,
deptno,
(case deptno
when 10 then
sal * 1.1
when 20 then
sal * 1.08
when 30 then
sal * 1.15
else
sal * 1.2
end) raisesal
from emp;

5.组函数

----1.sum() count() max() min() avg() 对确定的结果集求组函数
----2.计算组函数,null值不参与运算
----3.组函数不能与非分组字段一起使用,组函数只能和组函数一起使用,不能与其他普通字段一起使用 group by
----4.组函数不能使用在where后面

–统计一下一共有多少员工
select count(deptno) from emp;
select count(empno) from emp;
select count(1) from emp
–统计员工存在的部门总数
select count(distinct deptno) from emp;
select count(distinct 1) from emp;
– 统计20部门一共有多少人
select count(1) from emp where deptno=20;
– 计算本公司每个月一共要在工资上花费多少钱
select sum(sal) from emp;
– 计算20部门每个月的工资花销
select sum(sal) from emp where deptno=20;
– 查询本公司的最高工资和最低工资
select max(sal),min(sal),sum(sal),avg(sal) from emp;
–查看30部门的最高工资和最低工资
select max(sal),min(sal) from emp where deptno=30;
– 计算出所有员工的奖金总和
select sum(comm) from emp;
– 统计有奖金的员工有几个
select count(1) from emp where comm is not null;
–查询 最高薪水的员工姓名, 及薪水
select max(sal) from emp; --薪资最大值
select * from emp where sal = (select max(sal) from emp);
– 查询工资低于平均工资的员工编号,姓名及工资
select empno,ename,sal from emp where sal<(select avg(sal) from emp)

课后练习
查看高于本部门平均薪水员工姓名

select ename from emp e1 where sal>(select avg(sal) from emp e2 where e1.deptno = e2.deptno);

6.分组(group by)

格式:

select 查询数据 from 数据来源 where 行过滤条件 group by 分组字段1,分组字段2,...having 组过滤条件 order by 排序字段...;

执行流程: from -->where --> group by --> having --> select --> order by

注意:如果一旦分组,select后只能为分组字段或者组函数

– 找出20部门和30部门的最高工资
select max(sal) from emp where deptno in(20,30);
select max(sal),deptno from emp group by deptno having deptno in (20,30);  --先分组后过滤
select max(sal),deptno from emp where deptno in (20,30) group by deptno;  --先过滤后分组
– 求出每个部门的平均工资
select avg(sal) from emp group by deptno;
– 求出每个部门员工工资高于1000的的平均工资
select avg(sal) from emp where sal>1000 group by deptno;
– 求出10和20部门部门的哪些工资高于1000的员工的平均工资
select avg(sal) from emp where sal>1000 and deptno in(10,20) group by deptno;
select avg(sal) from emp where sal>1000 group by deptno having deptno in(10,20);
– 找出每个部门的最高工资
select max(sal),deptno from emp group by deptno;
– 求出平均工资高于2000的部门编号和平均工资
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
–按 岗位查询 平均工资,且平均工资大于2000的岗位
select avg(sal),job from emp group by job having avg(sal)>2000;
–先求出所有岗位的平均薪资和岗位,再判断平均薪资是否>2000
select * from (select avg(sal) avg_sal,job from emp group by job) where avg_sal>2000;
–查询 最低平均工资的部门编号 –所有部门中最低平均工资
select min(avg(sal)) from emp group by deptno;
–求出每个部门的平均工资和部门编号
select avg(sal),deptno from emp group by deptno;
–平均工资最低的部门
select avg(sal), deptno
from emp
group by deptno
having avg(sal) = (select min(avg(sal)) from emp group by deptno);

select *
from (select avg(sal) avg_sal, deptno from emp group by deptno)
where avg_sal = (select min(avg(sal)) from emp group by deptno);
  • 点赞
  • 收藏
  • 分享
  • 文章举报
sky_zed 发布了5 篇原创文章 · 获赞 0 · 访问量 89 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: