SELECTtname,SUM ( A.nnum ) nnum,A.ddate,A.pk_deptdoc,A.pk_calbody,A.deptname
FROM(SELECT A.ddate,A.pk_calbody,A.pk_deptdoc,A.nnum,b.target_name AS tname,d.deptname FROMkpi_gatherA JOIN kpi_target b ON A.pk_target = b.pk_targetLEFT JOIN bd_deptdoc d ON d.pk_deptdoc = A.pk_deptdoc WHEREA.dr = 0 AND b.dr = 0 AND A.pk_calbody = '1001B21000000087IIIY' AND ( b.pk_parent <> '0001D006FQ4T0NHCN681' OR b.pk_parent IS NULL ) AND A.ddate LIKE'2024-06%' UNION ALLSELECT A.ddate,A.pk_calbody,NULL pk_deptdoc,A.nnum,'酸耗' AS tname,NULL deptname FROMkpi_gatherA JOIN kpi_target b ON A.pk_target = b.pk_target WHEREA.dr = 0 AND b.dr = 0 AND A.pk_calbody = '1001B21000000087IIIY' AND b.pk_parent = '0001D006FQ4T0NHCN681' AND A.ddate LIKE'2024-06%' ) A
GROUP BYA.tname,A.ddate,A.pk_calbody,A.pk_deptdoc,A.deptname
ORDER BYA.tname DESC