0

看起来我的问题不够清楚,所以我尝试重写它:

我有一个“文件”表,其中包括“专辑”和“评级”等字段。每个专辑在文件表中都有给定数量的条目,每个条目都有一个等级。分级文件的分级 > 0。

我想要的是获得每张专辑的平均评分(这可以通过下面的查询来完成)。但是,我只想包括其文件的至少给定百分比(例如 75%)被评级的专辑(评级 > 0)

select album, avg(rating) AS avgRating
from file 
group by album 
order by avgRating DESC

有人可以帮助添加缺少的条款。

4

4 回答 4

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%。

于 2013-06-27T13:22:53.547 回答
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
于 2013-06-27T13:59:24.910 回答
1

如果您的意思是平均评分,您需要HAVING avg(rating) > 0

于 2013-06-27T13:05:23.237 回答
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
于 2013-06-27T13:09:31.310 回答