-- 自来水公司数据查询语句
--简单查询
select * from t_owners;
-- 查询水表编号为30408的业主记录,精确查询
select * from t_owners where watermeter='30408';
-- 查询业主名称包含'刘'的业主记录,模糊查询
select * from t_owners where name like '%刘%';
-- 查询业主包含'刘'的并且门牌号包含5的业主记录
select * from t_owners where name like '%刘%' and housenumber like '%5%';
-- 查询业主包含'刘'的或者门牌号包含5的业主记录
select * from  t_owners where name like '%刘%' or housenumber like '%5%';
-- 查询业主包含'刘'的或者门牌号包含5的业主记录,并且地址编号为3的记录
select * from  t_owners where (name like '%刘%' or housenumber like '%5%') and addressid=3;
-- 查询台账信息
select * from t_account;
-- 查询台账记录中用水记录大于1000,并且小于等于2000的记录
select * from t_account where usenum>=1000 and usenum<=2000;
select * from t_account where usenum between 1000 and 3000;
-- 查询价格表
select * from t_pricetable;
-- 查询t_pricetable表中maxnum为空的记录
select * from t_pricetable where maxnum is null;
-- 查询t_pricetable表中maxnum不为空的记录
select * from t_pricetable where maxnum is not null;
-- 查询业主表中的地址id ,不重复显示
select distinct addressid from t_owners;
-- 对t_account表按照用量进行升序排序
select * from t_account order by usenum;
-- 对t_account表按照用量进行降序排序
select * from t_account order by usenum desc;
-- 查询区域
select rowid ,t.* from t_area t;
-- 通过制定的rowid来查询
select rowid,t.* from t_area t where rowid='AAAM1TAAGAAAAA+AAA';
-- 查询用户类型信息
select rownum, t.* from t_ownertype t;
-- 统计2012年所有用户的用水量
select sum(usenum) from t_account where year='2012';
-- 统计2012年所有用户的平均用水量
select avg(usenum) from t_account where year='2012';
-- 统计2012年最高用水量的业主名称
select  t.name ,o.usenum  from t_owners t,t_account o
where o.usenum=(select max(o.usenum) as maxuse from t_account o where year='2012') and t.id=o.ownerid;
-- 统计2012年最小用水量的业主名称
select  t.name ,o.usenum  from t_owners t,t_account o
where o.usenum=(select min(o.usenum) as maxuse from t_account o where year='2012') and t.id=o.ownerid;
-- 统计业主类型id为1的业主数量
select count(*) from t_ownertype t where t.id=1 ;
-- 按区域分组统计水费的合计数
select areaid as 区域,sum(usenum) as 总水费 from t_account group by  areaid;
-- 查询水费合计大于16900的区域及水费合计
select areaid as 区域,sum(usenum) as 总水费 from t_account group by  areaid having sum(usenum)>70000;
-- 查询显示业主编号,业主名称,业主类型的名称
select t.id,t.name,o.name from t_owners t,t_ownertype o where t.ownertypeid=o.id;
-- 查询显示业主编号,业主名称,业主类型的名称,地址
select t.id,t.name,o.name, m.name 地址名称 from t_owners t,t_ownertype o,t_address m
where t.ownertypeid=o.id and t.addressid=m.id;
-- 查询显示业主编号,业主名称,地址,所属区域,业主类型
select t.id 业主编号,t.name 业主名称,ad.name 地址,ar.name 区域名称,ot.name 业主类型  from t_owners t,t_ownertype ot,t_address ad,t_area ar
where t.ownertypeid=ot.id and t.addressid=ad.id and ad.areaid=ar.id;
-- 查询显示业主编号,业主名称,地址,所属区域,业主类型,抄表员
select t.id 业主编号,t.name 业主名称,ad.name 地址,ar.name 区域名称,ot.name 业主类型,op.name 抄表员  
from t_owners t,t_ownertype ot,t_address ad,t_area ar,t_operator op
where t.ownertypeid=ot.id and t.addressid=ad.id and ad.areaid=ar.id and ad.operatorid=op.id;
-- 查询业主的财务记录,显示业主编号,名称,年,月,金额,如果此业主没有记录也要列出姓名
select t.id 业主编号,t.name 业主名称, year,month,money
from t_owners t left join t_account ac on t.id=ac.ownerid ;
-- Oracle特有方法
select t.id 业主编号,t.name 业主名称, year,month,money
from t_owners t , t_account ac where t.id=ac.ownerid(+) ;
-- 查询2012年1月用水量大于平均值得台账记录
select * from t_account
where year='2012' and month='01' and usenum>(select avg(usenum) from t_account where year='2012' and month='01');
-- 查询地址编号为1,3,4的业主信息
select * from t_owners t where t.id in(1,3,4);
-- 查询地址含有'花园'的业主信息
select t.* from t_owners t where t.addressid in(select id from t_address where name like '%花园%');
-- 查询地址不含有'花园'的业主信息
select t.* from t_owners t where t.addressid not in(select id from t_address where name like '%花园%');
-- 查询显示业主编号,业主名称,业主类型名称,条件为业主类型为'居民',使用子查询实现
select m.* from
(select t.id 业主编号,t.name 业主名称 ,ot.name 业主类型名称 from t_owners t,t_ownertype ot where t.ownertypeid=ot.id) m
where m.业主类型名称='居民';
-- 简单分页
-- 分页查找台账表t_account,每页显示10条记录
select rownum,t.* from t_account t where rownum<=10;
-- 显示第11条到20条的记录
select * from (select rownum r, t.* from t_account  t where rownum<=20 ) where r>=10;
-- 基于排序的分页
-- 分页查询台账表,每页10条记录,按使用字数降序排序,rownum是后于排序进行的
select * from
(select rownum r,t.*
from (select * from t_account order by usenum desc) t where rownum<=20)
where r>10;