SQL Server 查询好评率问题问题
数据库中有一评论表,包含 target_id(评论对象id),comment_level(评级,1,2,3分别代表 好评,中评,差评),其余评论人,时间等字段略,有没有不使用存储过程的情况下用sql语句查询出下面的结果
target_id haoping zhongping caping
123 85.5% 10% 4.5%
124 75% 20% 5%
比例结果为小数也行,请各位赐教
------解决方案--------------------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