示例我有一个电影表,上面有发行日期和长度。 我需要按降序选择发布年份和每年的平均电影长度。
我不知道我是从这里开始的:
SELECT YEAR(dateOfRelease), AVG(Lenght) FROM Movies WHERE ...
我知道这可能是一个简单的选择查询,但找不到解决方案。
答案 0 :(得分:1)
您要在此处使用GROUP BY:
SELECT YEAR(dateOfRelease), AVG(Lenght) FROM Movies
GROUP BY YEAR(dateOfRelease)
答案 1 :(得分:0)
您必须使用GROUP BY
子句按年汇总结果,并使用ORDER BY
子句对结果集进行排序,例如:
SELECT
YEAR(dateOfRelease) AS year,
AVG(Length) AS avg_length
FROM Movies
# WHERE ... put your conditions here
GROUP BY year
ORDER BY avg_length DESC