表:
名称 结果
abc pass
abc fail
abc pass
abc fail
aaa pass
aaa fail
bbb pass
期望查询出这样的效果:
名称 通过率
abc 50%
aaa 50%
bbb 100%
通过率=pass的出现次数 / 总数
------解决思路----------------------
SELECT 名称try this
,SUM(CASE WHEN 结果='pass' THEN 1 ELSE 0 END)*1.0/SUM(1)
FROM TB
GROUP BY 名称
------解决思路----------------------
select 名称
,cast(convert(decimal(18,2),cast(sum(case 结果 when 'pass' then 1 else 0 end) as decimal(18,2))/cast(count(1) as decimal(18,2)))*100
as varchar)+'%' 通过率
from table
group by 名称