自來水收費相關的sql語句題

1,統計某日的收費,按區域分組彙總

已知表 t_account (收費臺賬表)

在這裏插入圖片描述

已知表 t_area (區域表)

在這裏插入圖片描述

在這裏插入圖片描述

select (select name from t_area where ac.areaid = t_area.id ) 區域 ,
sum(ac.money) 收費, sum(ac.usenum)/1000 用水量噸
 from t_account ac
 where to_char(feedate,'yyyy-mm-dd') = '2012-08-14'  -- 某日
 group by ac.areaid  -- 按區域分組

2,統計某收費員某日的收費,按區域分組彙總

在這裏插入圖片描述

在這裏插入圖片描述

select (select name from t_area where ac.areaid = t_area.id ) 區域 ,
sum(ac.money) 收費, sum(ac.usenum)/1000 用水量噸
from t_account ac
where to_char(feedate,'yyyy-mm-dd') = '2012-08-14'  -- 某日
and ac.feeuser = 2 -- 某收費員
group by ac.areaid  -- 按區域分組

3,統計某年某月的收費記錄,按區域分組彙總

在這裏插入圖片描述

select (select name from t_area where ac.areaid = t_area.id ) 區域 ,
sum(ac.money) 收費, sum(ac.usenum)/1000 用水量噸
 from t_account ac
 where to_char(feedate,'yyyy-mm') = '2012-08'  -- 某年某月
 group by ac.areaid  -- 按區域分組

4,統計某收費員某年某月的收費記錄,按區域分組彙總

在這裏插入圖片描述

select (select name from t_area where ac.areaid = t_area.id ) 區域 ,
sum(ac.money) 收費, sum(ac.usenum)/1000 用水量噸
 from t_account ac
 where to_char(feedate,'yyyy-mm') = '2012-08'  -- 某年某月
 and ac.feeuser = 2 -- 某收費員
 group by ac.areaid  -- 按區域分組

5,統計某年收費情況,按區域分組彙總

在這裏插入圖片描述

select (select name from t_area where ac.areaid = t_area.id ) 區域 ,
sum(ac.money) 收費, sum(ac.usenum)/1000 用水量噸
 from t_account ac
 where to_char(feedate,'yyyy') = '2012'  -- 某年
 group by ac.areaid  -- 按區域分組

6,統計某年收費情況,按月份分組彙總

在這裏插入圖片描述

 select to_char(feedate,'mm') 月份 ,sum(money) 金額,  sum(usenum)/1000 使用噸數 from t_account  
 where to_char(feedate,'yyyy') = 2012 -- 某年
 group by to_char(feedate,'mm') -- 根據月份分組

7, 統計用水量,收費金額(分類型統計)

根據業主類型分別統計每種居民的用水量(整數,四捨五入)及收費金額
已知表:t_ownertype(業務類型表)

在這裏插入圖片描述

在這裏插入圖片描述

select ow.name,
 nvl( round(sum(usenum)/1000),0) "用水量(噸)" , nvl( sum(money),0) 金額
from T_OWNERTYPE ow ,T_ACCOUNT ac
where ow.id=ac.ownertype(+)  
group by ow.name

8,統計每個區域的業主戶數,並列出合計

已知表 t_owners(業主)

在這裏插入圖片描述

在這裏插入圖片描述


select ar.name 區域,count(ow.id) 業主戶數
from T_area ar ,t_owners ow,t_address ad
where ad.id=ow.addressid and ad.areaid=ar.id
group by ar.name
union all
select '合計',count(1) from T_OWNERS 

9,統計每個區域的業主戶數,如果該區域沒有業主戶數也要列出0

在這裏插入圖片描述

select ar.name 區域,count(s.id) 業主戶數
from t_area ar ,
(select ow.id,ow.name,ad.areaid from t_owners ow,t_address ad where ow.addressid=ad.id) s 
where ar.id=s.areaid(+)
group by ar.name

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章