select b.corporateOrgName, b.corporateOrgGuid companyId, count(case when
bc.ftype not in(1,2) then 1 else 0 end ) total, count(case when bc.ftype
in(3,4,5) then 1 else 0 end ) usetotal, count(case when bc.ftype = 6 then 1
else 0 end ) saveTotal, count(case when bc.ftype = 7 then 1 else 0 end )
returnTotal from B_seal_cycle bc join B_seal b on bc.sealId = b.id where
b.corporateOrgName like '%%' group by b.corporateOrgName,b.corporateOrgGuid

select b.corporateOrgName, b.corporateOrgGuid companyId, count(case when
bc.ftype not in(1,2) then 1 end ) total, count(case when bc.ftype in(3,4,5)
then 1 end ) usetotal, count(case when bc.ftype = 6 then 1 end ) saveTotal,
count(case when bc.ftype = 7 then 1 end ) returnTotal from B_seal_cycle bc join
B_seal b on bc.sealId = b.id where b.corporateOrgName like '%%' group by
b.corporateOrgName,b.corporateOrgGuid

<>遇到的问题

1、 对case when 语法，解读有误。

2、 count函数都会对不管1 或 0 进行统计。
3、 当加了else 0 之后，可以通过sum函数进行统计。

select b.corporateOrgName, b.corporateOrgGuid companyId, sum(case when
bc.ftype not in(1,2) then 1 else 0 end ) total, sum(case when bc.ftype
in(3,4,5) then 1 else 0 end ) usetotal, sum(case when bc.ftype = 6 then 1 else
0 end ) saveTotal, sum(case when bc.ftype = 7 then 1 else 0 end ) returnTotal
from B_seal_cycle bc join B_seal b on bc.sealId = b.id where b.corporateOrgName
like '%%' group by b.corporateOrgName,b.corporateOrgGuid

GitHub

Gitee