求每部电影的平均评分:
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
对于小提琴,看这里。