【发布时间】:2019-08-28 12:44:55
【问题描述】:
三张表:-Rating (rID, mID, stars, ratingDate) Movie (mID, title, year, director)Reviewer (rID, name)
找出 1980 年之前发行的电影的平均评分与 1980 年之后发行的电影的平均评分之间的差异。
(确保计算每部电影的平均评分,然后计算 1980 年之前和之后的电影的平均值。不要只计算 1980 年之前和之后的总体平均评分。)
但总是有null,即使我在where函数中过滤了null
''''
select r.mID,sum(stars),
count(case when year>1980 and stars is not null then stars end )as "numaf",
count(case when year<1980 and stars is not null then stars end )as "numbf",
sum(case when year>1980 and stars is not null then stars end )as "sumaf",
sum(case when year<1980 and stars is not null then stars end )as "sumbf",
(("sumaf"/"numaf")-("sumbf"/"numbf")) as "difof"
from Rating r left join movie m on r.mID = m.mID
where stars is not null
group by r.mID
''''
and this is output, always has "NULL":
101 9 0 3 <NULL> 9 <NULL>
103 5 0 2 <NULL> 5 <NULL>
104 5 2 0 5 <NULL> <NULL>
106 9 0 2 <NULL> 9 <NULL>
107 8 2 0 8 <NULL> <NULL>
108 10 3 0 10 <NULL> <NULL>
【问题讨论】:
-
结合大家(下)的建议,问题已经解决,非常感谢!
select max(p.avgV)-min(P.avgV) from(select avg(o.avgS) as avgV, case when year > 1980 then 1 else 0 end as yearIndicator from (select mID,avg(stars)as avgS from rating group by mID) as o join movie m on o.mID = m.mID group by yearIndicator) as P -
为什么会有这么多空值是因为对于某些电影,它们在 1980 年后才出现,所以即使我在连接两个表时过滤了空值,总和也必须为空。
标签: sql sqlite syntax-error