select bd_corp.unitname as 付款单位,
bd_cubasdoc.custcode as 收款单位编码,
bd_cubasdoc.custname as 收款单位名称,
sum(arap_djfb.bbye) as 付款总金额,
count(bd_corp.unitcode) as 付款次数
from bd_cubasdoc, arap_djfb, bd_cumandoc, arap_djzb, bd_corp
where arap_djfb.ksbm_cl = bd_cumandoc.pk_cumandoc
and bd_cumandoc.pk_cubasdoc = bd_cubasdoc.pk_cubasdoc
and arap_djfb.vouchid = arap_djzb.vouchid
and bd_corp.pk_corp = arap_djzb.dwbm
and arap_djfb.payflag in ('1', '2')
and arap_djfb.dr = '0'
and bd_cubasdoc.custprop <> 1 and length(bd_cubasdoc.custname)>4
group by bd_corp.unitname, bd_cubasdoc.custname, bd_cubasdoc.custcode
order by sum(arap_djfb.bbye) desc