看起来我的问题不够明确,所以我尝试重写它:
我有一个“文件”表,其中包括“专辑”和“评级”等字段。 每个专辑在文件表中都有一定数量的条目,每个条目都有一个评级。 评级文件的评级为> 0
我想要的是获得每张专辑的平均评分(这可以通过以下查询完成)。 但是,我只想要包含其文件至少给定百分比(例如75%)的专辑(等级> 0)
select album, avg(rating) AS avgRating
from file
group by album
order by avgRating DESC
有人可以帮助添加缺失的条款。
答案 0 :(得分:1)
如果您的意思是平均评分,则需要HAVING avg(rating) > 0
答案 1 :(得分:1)
试试这个
select album, avg(rating) AS avgRating from file group by album having avg(rating)>0 order by avgRating DESC
或
select album, avg(rating) AS avgRating from file group by album where rating>0 order by avgRating DESC
答案 2 :(得分:1)
select
album,
(case when sum(rating>0)>(count(*)/2) then avg(rating) else -1 end) AS avgRating
from file
group by album
having avgRating >= 0
order by avgRating DESC
count(*)/ 2< - 我的例子中你的百分比是50%。
答案 3 :(得分:1)
(更新,评论后):
select album, avg(rating) AS avgRating
from file
group by album
having sum(case when rating > 0 then 1 end)*1.0 / count(*) > 0.5
order by avgRating DESC