-----------1. 聚合函数-----------
--1 统计2012年所有用户的用水量总和
select sum(usenum) from t_account where year = 2012;
--2 统计2012年所有用水量(字数)的平均值
select avg(usenum) from t_account where year = 2012;
--3 统计2012年最高用水量(字数)
select max(usenum) from t_account where year = 2012;
--4统计2012年最低用水量(字数)
select min(usenum) from t_account where year = 2012;
--5统计记录个数
--count(*)会执行全表扫描,效率低下
select count(*) from t_account;
--count(1)等同于count(id)
select count(1) from t_account;
select count(2) from t_account;
select count(3) from t_account;
--只扫描id,所以效率比较高
select count(id) from t_account;
-- null不会被计数
select count(year) from t_account;
--------------2. 分组函数--------------------
--按区域分组统计2012年水费合计数
--当非聚合列和聚合列一起使用的时候,所有的非聚合列都要作为group by的子句
select areaid,sum(money) from t_account group by areaid;
--------------3. 分组后的条件筛选 having-----------------
-- 查询2012年水费合计大于159000的区域及水费合计
select areaid,sum(money) from t_account where year = 2012 group by areaid having sum(money)>159000
---------------4. 联系--------------------------------
--1.创建表
create table student(
stuid int ,
sname varchar2(32),
email varchar2(32) ,
age int check(age<=100 or age >= 0 ) ,
sex varchar2(3) check(sex='男' or sex = '女'),
birthday date,
introduce clob,
money number(10,2),
province varchar2(25)
);
alter table student add constraint pk_stuid primary key(stuid);
alter table student add constraint uq_eamil unique(email);
--2.插入数据
insert into student values(1,'tom','tom@126.com',23,'男',sysdate,'我叫tom',2000,'江苏');
insert into student values(2,'lucy','lucy@163.com',22,'女',sysdate,'我叫lucy',3000,'上海');
insert into student values(3,'jack','jack@163.com',24,'男',sysdate,'我叫jack',2000,'江苏');
insert into student values(4,'lily','lily@qq.com',21,'女',sysdate,'我叫lily',1500,'浙江');
insert into student values(5,'rose','rose@gmail.com',22,'女',sysdate,'我叫rose',1000,'安徽');
insert into student values(6,'harry','harry@126.com',23,'男',sysdate,null,320,'安徽');
--18.查询男生个数超过1个、账户余额总和超过1900的省份信息
select province,count(*),sum(money) from student where sex = '男' group by province
having count(*)>1 and sum(money)>1900
--19.查询账户余额总和最少的省份信息
--方式一
select a.* from
(select province,sum(money) from student group by province order by sum(money) asc) a
where rownum = 1;
--方式二
select province,sum(money) from student group by province
select province,sum(money) from student group by province having sum(money)=
(select min(sum(money)) from student group by province)
--方式三:all,所有
select province,sum(money) from student group by province having sum(money)<=
all(select sum(money) from student group by province)
----------------------5. 表连接--内连接-------------------------
--1查询显示业主编号,业主名称,业主类型名称
--方式一:隐式内连接,这虽不是sql99标准,但是大部分数据库都支持
select o.id 业主编号,o.name 业主名字,t.name 业主类型 from t_owners o , t_ownertype t where o.ownertypeid = t.id;
--方式二:内连接 inner join..on
select o.id,o.name,t.name from t_owners o inner join t_ownertype t on o.ownertypeid = t.id;
--2查询显示业主编号,业主名称、地址和业主类型(三表连接)
-- 方式一:隐式内连接
select o.id,o.name,ad.name,t.name from t_owners o,t_address ad,t_ownertype t
where o.addressid = ad.id and o.ownertypeid = t.id;
-- 方式二:
select o.id,o.name,ad.name,t.name from t_owners o inner join t_address ad on o.addressid = ad.id
inner join t_ownertype t on o.ownertypeid = t.id;
--3查询显示业主编号、业主名称、地址、所属区域、业主分类(4)
--4查询显示业主编号、业主名称、地址、所属区域、收费员、业主分类
------------------6. 表连接-左连接----------------
--查询业主的账务记录,显示业主编号、业主名称、年、月、金额。如果此业主没有账务记录也要列出姓名。
select o.id,o.name,ac.year,ac.month,ac.money from t_owners o left join t_account ac on o.id = ac.owneruuid;
------------------7. 表连接-右连接--------------------
--查询业主的账务记录,显示业主编号、业主名称、年、月、金额。如果账务记录没有对应的业主信息,也要列出记录。
select o.id,o.name,ac.year,ac.month,ac.money from t_owners o right join t_account ac on o.id = ac.owneruuid;
------------------8. 子查询------------------------
--查询2012年1月用水量大于平均值的台账记录
select * from t_account where year = 2012 and month = 1
and usenum>(select avg(usenum) from t_account)
--1查询地址编号为1 、3、4 的业主记录
select * from t_owners where addressid in (1,3,4);
--2查询地址含有“花园”的业主的信息
--方法一:表连接
select o.* from t_owners o,t_address ad where o.addressid = ad.id and ad.name like '%花园%';
--方式二:子查询
select o.* from t_owners o where addressid in (select id from t_address where name like '%花园%');
--3查询地址不含有“花园”的业主的信息
--方法一:表连接
select o.* from t_owners o,t_address ad where o.addressid = ad.id and ad.name not like '%花园%';
--方式二:子查询
select o.* from t_owners o where addressid in (select id from t_address where name not like '%花园%');
select o.* from t_owners o where addressid not in (select id from t_address where name like '%花园%');
--4查询2012年台账中,使用量大于2012年3月最大使用量的台账数据
--方式一
select * from t_account where usenum>
(select max(usenum) from t_account where year = 2012 and month = 3)
--方式二
select * from t_account where usenum>
all(select usenum from t_account where year = 2012 and month = 3)
select * from t_account where usenum >
any(select usenum from t_account where year = 2012 and month = 3)
------------9 ----------------
--嵌套子查询:在一个子查询中又包含了子查询
--查询在海淀区(t_area)的小区名字中含有花园的业主记录(t_owner)(t_address)
--方式一:表连接
select o.* from t_owners o,t_address ad,t_area ar where o.addressid = ad.id and ad.areaid = ar.id
and ar.name = '海淀' and ad.name like '%花园%';
--方式二:子查询
select o.* from t_owners o where o.addressid in
(select id from t_address where name like '%花园%' and areaid in
(select id from t_area where name = '海淀'))
------------------10. 标量子查询------------------------------
-- 概念:子查询的结果作为主查询的列显示
--查询台账表中的用户年用水量的总和 以及 年平均用水量
--方式一 :普通方式
select sum(usenum),avg(usenum) from t_account;
--方式二:标量子查询
--dual:oracle独有的,是虚表,只是用来显示数据的
select (select sum(usenum) from t_account) 年用水量总和,
(select avg(usenum) from t_account) 年平均用水量 from dual;
---------------10. 相关子查询--------------------------
--概念:子查询依赖主查询中的数据
--1查询显示业主编号,业主名称,业主类型名称
--方式一:隐式内连接
--方式二:内连接
--方式三:相关子查询
select o.id,
o.name,
(select name from t_ownertype where id = o.ownertypeid)
from t_owners o;
--2查询显示业主编号,业主名称、地址和业主类型
select o.id,
o.name,
(select name from t_address where id = o.addressid),
(select name from t_ownertype where id = o.ownertypeid)
from t_owners o;