我想通过电影获取具有最大“访问次数”值组的4行(*)的电影,字符名称和访问次数。
我的表(D)是这样的:
movie character_name Visits 1 Owen Lars 1 1 Obi-Wan Kanobi 2 1 Luke Skywalker 3* 2 Princess Leia 2 2 Luke Skywalker 3* 2 R2-D2 3* 3 Jabba the Hutt 1 3 Han Solo 2 3 Luke Skywalker 4*
我能想到的最佳编码是
select * FROM D group by D.movie HAVING max(Visits)
但我无法获得正确的行>
谁能告诉我应该怎么修改呢? 非常感谢!!
答案 0 :(得分:1)
SELECT
D.*
FROM
D
INNER JOIN
(
SELECT
MOVIE
, MAX(VISITS) AS VISITS
FROM D
GROUP BY MOVIE) F ON D.MOVIE = F.MOVIE AND D.VISITS = F.VISITS