SELECT t.title, Max(t.st)
FROM (SELECT title,
Avg(stars) AS st
FROM movie
JOIN rating USING(mid)
GROUP BY title)t;
这是我正在编写的查询,用于获取AVG(stars)
的最大值及其对应的标题。最高价值很好,但标题出现问题。我没有相应的标题。
子查询的输出为:
Avatar 4.0
E.T. 2.5
Gone with the Wind 3.0
Raiders of the Lost Ark 3.33333333333
Snow White 4.5
The Sound of Music 2.5
整个查询的输出错误。
The Sound of Music 4.5
预期/正确的输出是
SnowWhite 4.5
答案 0 :(得分:2)
尝试一下
select t.title,
t.st
from
(select title,
avg(stars) as st,rank() over(order by avg(stars) desc) as rSt
from movie
join rating using(mID)
group by title)t where t.rSt=1 ;
首先按降序对电影进行排名,然后在外部查询的条件下,选择具有最高排名的电影。希望这会有所帮助:-)
答案 1 :(得分:2)
您可以尝试使用相关子查询
select *
from
(select title,
avg(stars) as st
from movie a
join rating b on a.mID=b.mID
group by title
)t where st in
(select max(st) from (select title,
avg(stars) as st
from movie a
join rating b on a.mID=b.mID
group by title
)t1 on t.title=t1.title)
答案 2 :(得分:2)
您可以在查询中使用WHERE条件。您可以尝试联接,也可以只使用子查询。
Select title, max(stars)
from table
where stars = (select max(stars) from table)
或
SELECT top 1 title
FROM table
ORDER BY Stars DESC