查找每部电影SQL的平均评分

时间:2013-01-12 21:03:52

标签: sql database sqlite rating movie

我目前正在数据库上进行在线Standford课程,如果你能帮我解决这个SQL问题,我将非常感激。对不起,我是一个完整的菜鸟。

表格电影:

mID | title | year | director

表评级

rID | mID | stars | ratingDate

表审核员

rID | name

对于所有评论者对,以便两位评论者都对同一部电影进行评分,请返回两位评论者的姓名。消除重复,不要将审阅者与自己配对,并且只包括每对。对于每对,请按字母顺序返回对中的名称。

6 个答案:

答案 0 :(得分:1)

好。你实际上问了两个不同的问题。

首先,我会回答你在帖子标题中提出的问题。

这应该有效。

select Movie.title, avg(Rating.stars) as AR from
Rating join Movie on Rating.mID = Movie.mID
group by Movie.title
order by AR desc

如果你没有在连接中指定“on”子句,就像这样,

select Movie.title, avg(Rating.stars) as AR from
Rating join Movie
group by Movie.title
order by AR desc

然后所有电影都将具有相同的评级,即全球平均水平,因为加入的完整交叉产品。您希望在名称上使用内部联接,或者在此情况下为id。

其次,这是你问的另一个问题的答案,关于成对的评论者。 R1.name < R2.name确保审阅者姓名从左到右排序。并且order by R1.name确保这些对将从上到下排序。

select distinct R1.name, R2.name from
Rating Ra1 join Rating Ra2
join Reviewer R1 join Reviewer R2
where Ra1.mID = Ra2.mID and Ra1.rID != Ra2.rID
and R1.name < R2.name
and Ra1.rID = R1.rID and Ra2.rID = R2.rID
order by R1.name

答案 1 :(得分:0)

这将帮助您入门。

SELECT m.*, ra.*, re.*
FROM Movie m
JOIN Rating ra ON ra.mID = m.mID
JOIN Reviewer re ON re.rID = ra.rID

答案 2 :(得分:0)

也许是这样的:

SELECT rev.rID, rev.name, m.title
FROM [Reviewer] rev 
   JOIN [Rating] rate ON rev.rID = rate.rID
   JOIN [Movie] m ON rate.mID = m.mID
   JOIN (
     SELECT mID
     FROM [Rating] 
     GROUP BY mID
     HAVING COUNT(Distinct rID) > 1
   ) m2 on m.mID = m2.mID
ORDER BY rev.name

- 编辑

如果您还需要获得平均值:

SELECT rev.rID, rev.name, m.title, ar.avgRating
FROM [Reviewer] rev 
   JOIN [Rating] rate ON rev.rID = rate.rID
   JOIN [Movie] m ON rate.mID = m.mID
   JOIN (
     SELECT mID
     FROM [Rating] 
     GROUP BY mID
     HAVING COUNT(Distinct rID) > 1
   ) m2 on m.mID = m2.mID
   JOIN (
     SELECT mID, AVG(stars) as avgRating
     FROM [Rating] 
     GROUP BY mID
   ) ar on m.mID = ar.mID
ORDER BY rev.name

SQL Fiddle

答案 3 :(得分:0)

查看每部电影的平均评分:

SELECT 
    [m].[mID],
    [m].[title],
    AVG([r].[stars]) AS [AvgRating]
  FROM [Movie] [m]
  LEFT JOIN [Rating] [r] ON [m].[mID] = [r].[mID]
  GROUP BY 
    [m].[mID],
    [m].[title]

为每部电影找到独特的评论者:

SELECT
    [r].[mID],
    [v].[rID],
    [v].[name]
  FROM [Reviewer] [v]
  INNER JOIN [Rating] [r] ON [v].[rID] = [r].[rID]
  GROUP BY
    [v].[rID],
    [v].[name],
    [r].[mID]

联合:

SELECT 
  [rat].[mID],
  [rat].[title],
  [rev].[rID],
  [rev].[name],
  [rat].[AvgRating]
FROM
(
  SELECT 
    [m].[mID],
    [m].[title],
    AVG([r].[stars]) AS [AvgRating]
  FROM [Movie] [m]
  LEFT JOIN [Rating] [r] ON [m].[mID] = [r].[mID]
  GROUP BY 
    [m].[mID],
    [m].[title]
) AS [rat]
LEFT JOIN
(
  SELECT
    [r].[mID],
    [v].[rID],
    [v].[name]
  FROM [Reviewer] [v]
  INNER JOIN [Rating] [r] ON [v].[rID] = [r].[rID]
  GROUP BY
    [v].[rID],
    [v].[name],
    [r].[mID]
) AS [rev] ON [rat].[mID] = [rev].[mID]
ORDER BY 
  [rat].[mID] ASC,
  [rev].[name] ASC

对于小提琴,look here

答案 4 :(得分:0)

select distinct r2.name, r1.name
from 
(select r1.rid, r1.mid, r2.name
from (select * from reviewer order by name) r2
join (select r1.rid, r1.mid from rating r1 join reviewer r2 on r1.rid = r2.rid order by r2.name) r1
on r2.rid = r1.rid
group by mid
order by r2.name) r1,
(select r1.rid, r1.mid, r2.name
from reviewer r2,
rating r1
where r1.rid = r2.rid)r2
where r1.rid <> r2.rid and r1.mid = r2.mID
order by r2.name, r1.name

答案 5 :(得分:0)

我正在做同样的课程,这就是我得到的答案:

SELECT DISTINCT name1, name2
FROM (SELECT R1.name AS name1, R2.name AS name2
FROM (Rating JOIN Reviewer using(rId)) AS R1,
(Rating JOIN Reviewer using(rId)) AS R2
WHERE R1.mId = R2.mId AND R1.rId <> R2.rId) AS Tuple
WHERE name1 < name2;

我不知道为什么OP谈论平均标题,因为这个问题没有要求它。

编辑:进一步参考,这是数据:https://class.stanford.edu/c4x/DB/SQL/asset/moviedata.html