select target_id,count(case when commnet_level=1 then 1 else null end )*1.0/count(commnet_level) [haoping] from tb group by target_id后面两个同理
select target_id, sum(case when comment_level=1 then 1 else 0 end)*1.0/count(*) as haoping, sum(case when comment_level=2 then 1 else 0 end)*1.0/count(*) as zhongping, sum(case when comment_level=3 then 1 else 0 end)*1.0/count(*) as chaping from 评论表 group by target_id
from tb
group by target_id后面两个同理
sum(case when comment_level=1 then 1 else 0 end)*1.0/count(*) as haoping,
sum(case when comment_level=2 then 1 else 0 end)*1.0/count(*) as zhongping,
sum(case when comment_level=3 then 1 else 0 end)*1.0/count(*) as chaping
from 评论表
group by target_id