Finding the average rating of each movies SQL

荒凉一梦 提交于 2020-01-03 06:40:12

问题


I'm currently taking the online standford class on databases, If you could help me solve this sql problem I would greatly appreciate it. Sorry I'm a complete noob.

Table Movie:

mID | title | year | director

Table Rating

rID | mID | stars | ratingDate

Table Reviewer

rID | name

For all pairs of reviewers such that both reviewers gave a rating to the same movie, return the names of both reviewers. Eliminate duplicates, don't pair reviewers with themselves, and include each pair only once. For each pair, return the names in the pair in alphabetical order.


回答1:


Okay. You actually asked two different questions.

First, I'll answer the question you asked in the title of the post.

This should work.

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

If you don't specify the "on" clause in the join, like this,

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

then all movies will have the same rating, the global average, to due the full cross product of the join. You want an inner join on name or, in this case, id.

Second, here is the answer to the other question you asked, about pairs of reviewers. R1.name < R2.name ensures the reviewer names will be sorted left to right. and order by R1.name ensures those pairs will be sorted top to bottom.

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



回答2:


This will get you started.

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



回答3:


Find the average rating per movie:

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]

Find the unique reviewers per movie:

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]

Combine:

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

For a Fiddle, look here.




回答4:


Maybe something like this:

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

--EDIT

If you need to get the average as well:

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

And the SQL Fiddle.




回答5:


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



回答6:


I'm doing the same course and this is the answer I got:

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;

I don't know why OP talk about average on title, since this question don't ask for it.

EDIT: for further reference, here's the data: https://class.stanford.edu/c4x/DB/SQL/asset/moviedata.html



来源:https://stackoverflow.com/questions/14297937/finding-the-average-rating-of-each-movies-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!