--查出所有现金中心的单位ID
with
AllUnit
as(
select t.ORGANIZATIONID orgid,t.parentid
from CDMS_ORGANIZATION t where T.CATEGORY = 4
start with t.organizationid = ‘05e85693-14b0-4582-8063-8fbde85371f0‘
connect by t.parentid = PRIOR t.organizationid),
N_instock
as(
-- instock 完整money,残损money,字段:入库单位ID,mone1,money2
select inunitid,(baint+bunint+intaintNum+baoInt) intMoney,(baDMA+BUNDMA+baoDMA) DMAMoney
from(
select tba.INUNITID,
NVL((case ba.ISDAMAGE when 0 then 100*ba.valuta end),0) as BaInt,
NVL((case ba.ISDAMAGE when 1 then 100*ba.valuta end),0) as BaDmA,
NVL((case bun.ISDAMAGE when 0 then 1000*bun.valuta end),0) as BunINT,
NVL((case bun.ISDAMAGE when 1 then 1000*bun.valuta end),0) as BUNDMA,
NVL(inta.COUNTER*inta.VALUTA,0) intaintNUM,NVL(pack.INTACTNUM,0) baoint,
NVL(pack.DAMAGENUM,0) baodma from
(
select it.inunitid,de.barcode,de.UNIT from GZH_INSTOCK it
left join GZH_INSTOCK_DETAIL de on it.BILLID=de.BILLID
--where to_char(it.billdate,‘yyyy-MM‘)=‘2016-11‘ and intype=‘1311‘
)tba
left join GZH_BAINFO ba on tba.BARCODE=ba.BARCODE and tba.unit=1
left join GZH_BUNCHINFO bun on tba.BARCODE=bun.BUNCHCODE and tba.unit=2
left join GZH_PACHETINFO pack on tba.barcode=pack.RFIDNO and tba.unit=4
left join GZH_INTACTBOXINFO inta on tba.BARCODE=inta.BARCODE and tba.unit=3
)
)
,
allData
as(
select t.PARENTID,NVL(sum(t1.FLITTINGMONEY),0) outmoney,
NVL(sum(t2.INTMONEY),0) INTMONEY,
NVL(sum(t2.DMAMONEY),0) DMAMONEY from allunit t
left join GZH_outsTOCK t1 on t.orgid=t1.outUNITID
left join N_instock t2 on t.orgid=t2.inUNITID
--where to_char(t1.billdate,‘yyyy-MM‘)=‘2016-11‘ and outtype=‘1321‘
group by t.PARENTID
)
,
TFinfo
as
(
select parentid,
decode((select sum(outmoney) from allData),0,0,outmoney/(select sum(outmoney) from allData)) TFscale,
decode((select sum(intmoney) from allData),0,0,intmoney/(select sum(intmoney) from allData)) HLWscale,
decode((select sum(dmamoney) from alldata),0,0,dmamoney/(select sum(dmamoney) from alldata)) HLCscale
from alldata)
select fullname,TFscale 投放占比,HLWscale 回笼完整占比,HLCscale 回笼完整占比
from TFinfo left join cdms_organization
on cdms_organization.organizationid=tfinfo.parentid;
上面的这段sql是尹哥教我的,虽然不是自己写的,但是按照这个思路,我发现我之前是对的,只是对oracle的认识不够,写不出来
今天最主要的是都让我重新的认识了oracle的查询,和我之前学的sql查询的差距还是挺大的,各种各样的结构的选择和各种各样的代码的编写
在计算比例的时候,我们先对数据进行求和,为了不能表中出现空的字符,我们选用了nvl(字段名,0),这是将空的字符串变为O 的语句。
今天我还用了一个很神奇的方法,decode(a,0,0,b/a*100)这个是为了不让除数为0 的时候,使oracle报错,这个让我的语句更快的写完。
我将这两个用法记录下来就是为了让自己能更好的记住这两个词的用法。