sql server 中使用sum函数实现统计

select MA001 as 供应商编号,MA003 as 供应商全称,

sum(case when TAC04>0 then 1 else 0 end)as 总笔数,--当字段大于0时则为1否则为0,所有值求和

sum(case when TAC01>0 then 1 else 0 end)AS 验收笔数,

sum(case when TA008>0 then 1 else 0 end) as 验退笔数

from QMSTA left join PURCC on CC001=TA001 AND CC002=TA002

LEFT JOIN PURMA ON CC005=MA001

WHERE TA014='Y'

group by MA001,MA003

ORDER BY MA001,MA003

wKioL1agbJHRlsQEAAAtdMr_B6Q978.png