5月27日oracle

1.伪列:rownum ,rowid
2.聚合函数(聚合函数是对组的聚合,如果没有分组,是对这个表的聚合)
1.sum 
2.avg
3.max
4.min
5.count


-- 分组聚合统计(select 后一定是分组聚合的条件,或者是聚合函数)
select sum(money),sum(sumne) from t_account group by areaid;


3.编写多表查询的语句
1.确定需要哪些表
from t_owners o, t_ownertype ow ,t_address ad ,t_area ar, t_operator op
2.确定连接条件

from t_owners o, t_ownertype ow ,t_address ad ,t_area ar, t_operator op

where o.ownertypeid=ow.id and o.addressid=ad.id and ad.areaid=ar.id and ad.operatorid = op.id
3.确定查询哪些字段
select o.name 业主名称 , o.id 业主编号 , ad.name 地址, ar.name 所属区域, op.name 收费员,ow.name 业主分类

from t_owners o, t_ownertype ow ,t_address ad ,t_area ar, t_operator op

where o.ownertypeid=ow.id and o.addressid=ad.id and ad.areaid=ar.id and ad.operatorid = op.id


4.oracle中的单引号括起来的表示字符串,用双引号括起来的表示字段/列 ,另外,不用引号括起来的也表示字段
5.select 子查询:
1.思路是这样的,就是先写出主查询,然后将查出的字段用子查询转化,有时候可能需要转化之后再转化


2.子查询是引用型查询,多表关联查询是平面查询,二者思路不一样,可以达到一样的效果;


6.伪列rownum的应有
1.rownum的产生时间:查询结果出来一条,rownum出现一个;跟着结果的产生而产生;
2.sql语句编写顺序, s---f---w---g---h---o
3.解析顺序         f---w---g---h---o---s  ,由于解析顺序,所以在o(order)之前表已经生成了,
而s(select)只是在o(order)之后排序结束的表中,选出需要的字段;o(order)之前:都是表生成
阶段:包括从哪里生成f(from),要满足哪些筛选条件w(where),怎么分组g(group),分组后有哪些
筛选条件h(having),最终生成了要查找的表;


7.语句练习
select name ,decode(ownertypeid,
1,'土豪',
2,'屌丝'
'其他'
)
from t_owners;


select owneruuid,money,(case
when money <=2000 then '屌丝'
when money<=8000 then '小土豪'
else '真土豪'
end
) 身份
form  t_owners;


8.行列转换,就是先分组聚合,然后将分组聚合的不同情况在一行显示,就把本来属于列的数据转换成行的数据;


9.上机练习题
1.select子查询
select areaid,sum(usenum),sum(money) from t_account ac group by ac.areaid;  用select的子查询,将areaid 转换为ar.name
select (select ar.name from t_area ar where ar.id=ac.areaid) 区域,sum(usenum) 用水量,sum(money) 金额
from t_account ac 
where to_char(feedate,'yyyy-mm-dd')='2012-05-14'
group by ac.areaid;

2.select子查询
select (select name from t_area where id=areaid) 区域, sum(usenum) 用水量, sum(money) 金额
from t_account 
where to_char(feedate,'yyyy-mm-dd')='2012-05-14' and feeuser=2
group by areaid;

3. select子查询
select (select ar.name from t_area ar where ar.id=ac.areaid) 区域,sum(ac.usenum) 用水量,sum(ac.money) 总花费
from t_account ac
where to_char(ac.feedate,'yyyy-mm')='2012-05'
group by ac.areaid;



5.内连接查询
select ac.areaid 区域, sum(ac.usenum) 用水量,sum(ac.money) 金额
  from t_account ac, t_area ar
where ac.areaid=ar.id and to_char(feedate,'yyyy')='2012'
group by ac.areaid; 

6.
select ac.month 月份, sum(ac.usenum) 使用吨数,sum(ac.money)金额 
from t_account ac 
where to_char(feedate,'yyyy')='2013'
group by to_char(feedate,'mm')
order by to_char(feedate,'mm');




7. 字段成列,集合运算产生行

select '用水量'   统计项,       
       sum (decode(to_char(feedate,'mm'),'01',usenum,0))一月份,
       sum (decode(to_char(feedate,'mm'),'02',usenum,0))二月份,
       sum (decode(to_char(feedate,'mm'),'03',usenum,0))三月份,
       
       sum (decode(to_char(feedate,'mm'),'04',usenum,0))四月份,
       sum (decode(to_char(feedate,'mm'),'05',usenum,0))五月份,
       sum (decode(to_char(feedate,'mm'),'06',usenum,0))六月份,
       
       sum (decode(to_char(feedate,'mm'),'07',usenum,0))七月份,
       sum (decode(to_char(feedate,'mm'),'08',usenum,0))八月份,
       sum (decode(to_char(feedate,'mm'),'09',usenum,0))九月份,
       
       sum (decode(to_char(feedate,'mm'),'10',usenum,0))十月份,
       sum (decode(to_char(feedate,'mm'),'11',usenum,0))十一月份,
       sum (decode(to_char(feedate,'mm'),'12',usenum,0))十二月份
from t_account
where to_char(feedate,'yyyy')='2013'

union
select '金额' 统计项,
       sum (case  when to_char(feedate,'mm')='01' then money else 0 end)一月份,
       sum (case  when to_char(feedate,'mm')='02' then money else 0 end)二月份,
       sum (case  when to_char(feedate,'mm')='03' then money else 0 end)三月份,
       
      
       sum (case  when to_char(feedate,'mm')='04' then money else 0 end)四月份,
       sum (case  when to_char(feedate,'mm')='05' then money else 0 end)五月份,
       sum (case  when to_char(feedate,'mm')='06' then money else 0 end)六月份,
       
       sum (case  when to_char(feedate,'mm')='07' then money else 0 end)七月份,
       sum (case  when to_char(feedate,'mm')='08' then money else 0 end)八月份,
       sum (case  when to_char(feedate,'mm')='09' then money else 0 end)九月份,
       
       sum (case  when to_char(feedate,'mm')='10' then money else 0 end)十月份,
       sum (case  when to_char(feedate,'mm')='11' then money else 0 end)十一月份,
       sum (case  when to_char(feedate,'mm')='12' then money else 0 end)十二月份
from t_account
where to_char(feedate,'yyyy')='2013'



8.
select ot.name name,nvl(round(sum(ac.usenum)),0) 用水量, nvl(round(sum(ac.money),2),0) 金额
from t_account ac , t_ownertype ow 
where ac.ownertype(+)=ot.id 
group by  ot.name;




9.
select ar.name 区域,count(wo.id)业主户数
from t_owners ow , t_address ad, t_area ar 
where ow.addressid=ad.id and ad.areaid=ar.id
group by ar.name
union
select '合计' ,count(1) from t_owners;


10.
select ar.name 区域, nvl(count(ow.id),0) 业主户数
from t_area ar, t_owners ow,t_address ad
where ow.addressid=ad.id and ar.id=ad.areaid(+)
group by ar.name

select ar.name 区域, nvl(count(ow.id),0) 业主户数
from t_area ar,(select ow.id ,ad.areaid from t_owners ow,t_address ad where ow.addressid=ad.id) p
where ar.id=p.areaid(+)
group by ar.name


拓展练习题
1.
select ename 姓名,sal 薪资
from emp 
where job in('clerk','manager') 



2.
select ename 姓名,deptno 部门号,sal 工资,job 工作
from emp
where deptno between 10 and 30




3.
select ename 姓名,sal 工资, job 职位
from emp
where ename like 'J%'




4.
select ename 姓名,job 工作,sal 工资
from emp
where sal<2000


order by sal desc


5.
select ename 姓名,sal 工资,e.deptno 部门号,dname 部门名称,loc部门地址
from emp e,dept d
where e.deptno=d.deptno and job='clerk'




6.
select ename 姓名,job 工作,sal 工资
from emp
where sal>(select sal from emp where ename= 'jones')




7. 方法一:差集运算
select ename 姓名,job 工作,deptno 部门号
from emp,dept
where emp.deptno(+)=dept.deptno
mins
select ename 姓名,job 工作,deptno 部门号
from emp,dept
where emp.deptno=dept.deptno

方法二:where子查询
select ename 姓名,job 工作,deptno 部门号
from emp
where dempno not in (select dempno from dept)




8.
select * 
from emp
where deptno in
(
select distinct deptno
from emp
where sal between 1000 and 3000
)






9. 第一种
select ename
from emp
where sal>= all(select max(sal) from emp)

第二种
select ename
from emp
where sal = (select max(sal) from emp)




10.
select ename 姓名,nvl(sum(sal+comm),0) 底薪+奖金
from emp




11.
select ename 姓名,sal 工资,dname部门
from emp e,dept d
where e.deptno=d.deptno and hiredate<to_date('1981-07-01','yyyy-mm-dd')




12. select子查询:
select (select dname from dept d where d.deptno=e.deptno) 部门名称,sum(emp.empno)员工数
from emp e
where hiredate>to_date('1981-07-01','yyyy-mm-dd')
group by e.deptno

连接查询
select d.dname 部门名称,sum(emp.empno)员工数
from emp e,dept d
where d.dempno=e.dempno hiredate>to_date('1981-07-01','yyyy-mm-dd')
group by d.dname




13. 连接查询
select ename 姓名,sal 工资
from emp e,dept d
where e.deptno=d.deptno and d.loc='chicago' and job in('manager','salesman')

子查询
select ename 姓名,sal 工资
from emp e
where job in('manager','salesman') and deptno in (select deptno from dept where loc ='chicago')


14. 此处巧妙地将年数转化为月数进行计算
select ename
from emp 
where   add_month(sysdate,-288) >=hiredate


15.
select sum(sal+nvl(comm,0)) 总收入
from emp
where to_char(hiredate,'yyyy')='1981'




16. trunc函数只能截取秒以上精度,to_char可以实现秒级别的对准
select enam , to_char(hiredate,'yyyy-mm-dd hh-mi-ss')
from emp




17.
select to_char(hiredate,'yyyy-mm') 录用时间, loc 地点, count(empno) 员工数量
from emp e,dept d
where e.deptno=d.deptno
group by  to_char(hiredate,'yyyy-mm'),loc


18.
select d.dname 部门名称 , e.name经理名字
from dept d,emp e
where d.dname=e.dname and job='manager'




19.
select 部门名称, max(平均薪资)
from 
(select d.dname 部门名称, avg(sal)平均薪资
from dept d,emp e
where d.deptno=e.deptno,
group by e.deptno)
union
select 部门名称, min(平均薪资)
from 
(select d.dname 部门名称, avg(sal)平均薪资
from dept d,emp e
where d.deptno=e.deptno,
group by e.deptno)




20.
select
from
where
group by
having
order by















猜你喜欢

转载自blog.csdn.net/weixin_36898943/article/details/80754547