欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

处理本期同期上期的一个sql

程序员文章站 2022-03-03 23:52:07
...
--处理本期同期上期的一个sql
select COALESCE(benqi.area,shangqi.area,tongqi.area) as AllArea,COALESCE(benqi.company,shangqi.company,tongqi.company) as AllCompany,
nvl(benqi.orderNum,0) as benqiOraerNum,nvl(benqi.needNum,0) as benqiNeedNum,nvl(benqi.amount,0) as benQIAmout,
nvl(shangqi.orderNum,0) as shangqiOraerNum,nvl(shangqi.needNum,0) as shangqiNeedNum,nvl(shangqi.amount,0) as shangqiAmout,
nvl(tongqi.orderNum,0) as tongqiOraerNum,nvl(tongqi.needNum,0) as tongqiNeedNum,nvl(tongqi.amount,0) as tongqiAmout
from
(select cig.area as area,cig.company as company,sum(agg.order_num) as orderNum,sum(agg.need_num) as needNum,sum(agg.amount) as amount
from dim_tel_sale_cigarette cig,fact_tel_sale_aggr agg,dim_tel_sale_area area
where (cig.cig_id = agg.cig_id and agg.area_id = area.area_id) and (substr(agg.time_id,0,6) between 200901 and 200907)
group by cig.area,cig.company) benqi FULL OUTER JOIN
(select cig.area as area,cig.company as company,sum(agg.order_num) as orderNum,sum(agg.need_num) as needNum,sum(agg.amount) as amount
from dim_tel_sale_cigarette cig,fact_tel_sale_aggr agg,dim_tel_sale_area area
where (cig.cig_id = agg.cig_id and agg.area_id = area.area_id) and (substr(agg.time_id,0,6) between 200807 and 200812)
group by cig.area,cig.company) shangqi on (benqi.area = shangqi.area and benqi.company = shangqi.company)
FULL OUTER JOIN
(select cig.area as area,cig.company as company,sum(agg.order_num) as orderNum,sum(agg.need_num) as needNum,sum(agg.amount) as amount
from dim_tel_sale_cigarette cig,fact_tel_sale_aggr agg,dim_tel_sale_area area
where (cig.cig_id = agg.cig_id and agg.area_id = area.area_id) and (substr(agg.time_id,0,6) between 200801 and 200807)
group by cig.area,cig.company) tongqi
on (benqi.area = tongqi.area and benqi.company = tongqi.company)
order by AllArea,AllCompany
相关标签: SQL