根据上级部门查本级及下级的sql

select 
aa.dept_id, aa.dept_name,c.dept_name as parentName,
sum(bb.predictCharge) as predictCharge,
sum(bb.actualCharge) as actualCharge,
sum(bb.countyAllot) as countyAllot,
sum(bb.townAllot) as townAllot,
sum(bb.villageAllot) as villageAllot
from sys_dept aa 
left join ( 
SELECT 
b.dept_id, d.ancestors,d.dept_name,
 round( sum( bill_amount ), 2 ) AS predictCharge,
	round( sum( IF ( trade_status = '2', bill_amount, 0 ) ), 2 ) AS actualCharge,
round (sum( IF ( trade_status = '2', bill_amount, 0 ) )-FLOOR( sum( IF ( trade_status = '2', bill_amount, 0 ) ) * 0.2 ) - FLOOR( sum( IF ( trade_status = '2', bill_amount, 0 ) ) * 0.3 ),2) AS countyAllot, 
	FLOOR( sum( IF ( trade_status = '2', bill_amount, 0 ) ) * 0.2 ) AS townAllot, 
	FLOOR( sum( IF ( trade_status = '2', bill_amount, 0 ) ) * 0.3 )  as villageAllot 
FROM nyys_bill b 
JOIN sys_dept d ON b.dept_id = d.dept_id where b.del_flag = '0' AND bill_year = 2023 group by dept_id  
) bb on aa.dept_id = bb.dept_id or FIND_IN_SET(aa.dept_id, bb.ancestors) 
LEFT JOIN sys_dept c on aa.parent_id=c.dept_id 
where aa.parent_id =100
group by aa.dept_id 
 ORDER BY  aa.dept_id
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值