SQL Inner Join 2表:基于Group的AVG

问题描述 投票:0回答:1

我有2个表:电影和使用以下架构的演员:

movies: | id | name | score |
cast:   | movie_id | cast_id | cast_name | 
-- cast.movie_id corresponds with movies.id

我想在某些条件下找到平均电影分数最高的演员(评分<40的电影应该被忽略,选中的演员应该出现在> 2部电影中)。这就是我现在拥有的:

SELECT c.cast_id, c.cast_name, AVG(m.score) AS average_score FROM movies AS m
INNER JOIN 
    (SELECT * FROM cast GROUP BY cast_id HAVING COUNT(cast_id) > 2) AS c
ON m.id == c.movie_id
GROUP BY c.cast_id
HAVING m.score >= 40
ORDER BY AVG(m.score) DESC, c.cast_name ASC
LIMIT 10;

但是当我检查结果时,例如对于某些演员来说,输出平均分为100.0,但他的电影的分数分别为100,100,85。我可以知道上述SELECT语句出错了吗?

sql sqlite
1个回答
0
投票

你可以这样说:

SELECT c.cast_id, c.cast_name, AVG(m.score) AS average_score 
FROM movies m INNER JOIN
     cast c
     ON m.id = c.movie_id INNER JOIN
     (SELECT c.cast_id
      FROM cast c
      GROUP BY c.cast_id 
      HAVING COUNT(*) > 2  -- Only need `COUNT(DISTINCT c.movie_id)` if a person can be in one movie more than once
     ) cc
     ON cc.cast_id = c.cast_id
WHERE m.score >= 40
GROUP BY c.cast_id, c.cast_name
ORDER BY average_score DESC, c.cast_name
LIMIT 10;

笔记:

  • 子查询用于计算给定演员的电影。
  • 需要额外的JOINcast来获取id和名字。
  • HAVING条款是不必要的。
© www.soinside.com 2019 - 2024. All rights reserved.