当前位置:网站首页>Oracle exercise

Oracle exercise

2022-06-26 02:56:00 qq_ forty-five million eight hundred and forty-nine thousand tw

ORACLE Chapter II answers to after class exercises

by 《 Tap water charging system 》 Develop functions related to the statistics module
1. Daily charge sheet ( total )
Count the charges for a day , Summarize by region , The effect is as follows :

sentence :
select (select name from T_AREA where id= areaid ) Area ,
sum(usenum)/1000 “ water consumption ( Tons of )” ,sum(money) amount of money
from T_ACCOUNT
where to_char(feedate,‘yyyy-mm-dd’)=‘2012-05-14’
group by areaid

2. Daily charge sheet ( Toll collector )
Count the charges of a toll collector on a certain day , Summarize by region , The effect is as follows :

sentence :
select (select name from T_AREA where id= areaid ) Area ,
sum(usenum)/1000 “ water consumption ( Tons of )” ,sum(money) amount of money
from T_ACCOUNT
where to_char(feedate,‘yyyy-mm-dd’)=‘2012-05-14’
and feeuser=2
group by areaid

3. Monthly charge Report ( total )
Count the charging records of a certain month in a certain year , Summarize by region

sentence :
select (select name from T_AREA where id= areaid ) Area ,
sum(usenum)/1000 “ water consumption ( Tons of )” ,sum(money) amount of money
from T_ACCOUNT
where to_char(feedate,‘yyyy-mm’)=‘2012-05’
group by areaid

4. Monthly charge Report ( Toll collector )
Count the charging records of a toll collector in a year and a month , Summarize by region
sentence :
select (select name from T_AREA where id= areaid ) Area ,
sum(usenum)/1000 “ water consumption ( Tons of )” ,sum(money) amount of money
from T_ACCOUNT
where to_char(feedate,‘yyyy-mm’)=‘2012-05’ and feeuser=2
group by areaid

5. Annual statement of charges ( Sub regional statistics )
Statistics of charges in a certain year , Summarize by region , The effect is as follows :

sentence :
select (select name from T_AREA where id= areaid ) Area ,
sum(usenum)/1000 “ water consumption ( Tons of )” ,sum(money) amount of money
from T_ACCOUNT
where to_char(feedate,‘yyyy’)=‘2012’
group by areaid

6. Annual statement of charges ( Statistics by month )
Statistics of charges in a certain year , Summarize by month , The effect is as follows

sentence :
select to_char(feedate,‘mm’) month ,sum(usenum)/1000 Using tons ,sum(money) amount of money
from T_ACCOUNT
where to_char(feedate,‘yyyy’)=‘2013’
GROUP BY to_char(feedate,‘mm’)
ORDER BY to_char(feedate,‘mm’)

7. Annual statement of charges ( Statistics by month )
Statistics of charges in a certain year , Summarize by month , The effect is as follows

sentence :
select ‘ water consumption ( Tons of )’ Statistical items ,
sum (case when to_char(feedate,‘mm’)=‘01’ then usenum else 0 end )/1000 January ,
sum (case when to_char(feedate,‘mm’)=‘02’ then usenum else 0 end )/1000 February ,
sum (case when to_char(feedate,‘mm’)=‘03’ then usenum else 0 end )/1000 March ,
sum (case when to_char(feedate,‘mm’)=‘04’ then usenum else 0 end )/1000 April ,
sum (case when to_char(feedate,‘mm’)=‘05’ then usenum else 0 end )/1000 May ,
sum (case when to_char(feedate,‘mm’)=‘06’ then usenum else 0 end )/1000 June ,
sum (case when to_char(feedate,‘mm’)=‘07’ then usenum else 0 end )/1000 July ,
sum (case when to_char(feedate,‘mm’)=‘08’ then usenum else 0 end )/1000 August ,
sum (case when to_char(feedate,‘mm’)=‘09’ then usenum else 0 end )/1000 September ,
sum (case when to_char(feedate,‘mm’)=‘10’ then usenum else 0 end )/1000 October ,
sum (case when to_char(feedate,‘mm’)=‘11’ then usenum else 0 end )/1000 November ,
sum (case when to_char(feedate,‘mm’)=‘12’ then usenum else 0 end )/1000 December
from T_ACCOUNT
where to_char(feedate,‘yyyy’)=‘2013’
UNION ALL
select ‘ amount of money ( element )’ Statistical items ,
sum (case when to_char(feedate,‘mm’)=‘01’ then money else 0 end ) January ,
sum (case when to_char(feedate,‘mm’)=‘02’ then money else 0 end ) February ,
sum (case when to_char(feedate,‘mm’)=‘03’ then money else 0 end ) March ,
sum (case when to_char(feedate,‘mm’)=‘04’ then money else 0 end ) April ,
sum (case when to_char(feedate,‘mm’)=‘05’ then money else 0 end ) May ,
sum (case when to_char(feedate,‘mm’)=‘06’ then money else 0 end ) June ,
sum (case when to_char(feedate,‘mm’)=‘07’ then money else 0 end ) July ,
sum (case when to_char(feedate,‘mm’)=‘08’ then money else 0 end ) August ,
sum (case when to_char(feedate,‘mm’)=‘09’ then money else 0 end ) September ,
sum (case when to_char(feedate,‘mm’)=‘10’ then money else 0 end ) October ,
sum (case when to_char(feedate,‘mm’)=‘11’ then money else 0 end ) November ,
sum (case when to_char(feedate,‘mm’)=‘12’ then money else 0 end ) December
from T_ACCOUNT
where to_char(feedate,‘yyyy’)=‘2013’

8. Statistical water consumption , Amount of charge ( Statistics by type )
The water consumption of each type of residents shall be counted according to the type of owner ( Integers , rounding ) And the amount charged , If this type has no data in the account table, the value must also be listed as 0 The record of , The effect is as follows :

sentence :
select ow.name,
nvl( round(sum(usenum)/1000),0) “ water consumption ( Tons of )” , nvl( sum(money),0) amount of money
from T_OWNERTYPE ow ,T_ACCOUNT ac
where ow.id=ac.ownertype(+)
group by ow.name

analysis : The knowledge points used here include the left outer connection 、sum()、 grouping group by 、round() and nvl()

9. Count the number of owners in each area , And list the total

sentence :
select ar.name Area ,count(ow.id) Number of owners
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 ‘ total ’,count(1) from T_OWNERS

10. Count the number of owners in each area , If there is no owner in the area, the number of households should also be listed 0
Pictured :

sentence :
select ar.name Area ,count(owad.id) Number of owners
from T_AREA ar ,
(
select ow.id,ow.name,ad.areaid from T_OWNERS ow,T_ADDRESS ad where ow.addressid=ad.id
)
owad
where ar.id=owad.areaid(+)
group by ar.name

原网站

版权声明
本文为[qq_ forty-five million eight hundred and forty-nine thousand tw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260129559280.html