我的查询:
SELECT P.lastname, P.firstname, MAX(MD.movie_id)
FROM Person AS P
INNER JOIN Movie_Directors AS MD ON P.person_id = MD.person_id
INNER JOIN Movie AS M ON M.movie_id = MD.movie_id
GROUP BY P.firstname, P.lastname, MD.movie_id
HAVING MAX(MD.movie_id);
我收到此错误:
在需要条件的上下文中在“;”附近指定的非布尔类型的表达式。
我需要为下一个问题选择一个SQL,我不明白该错误有人能帮忙吗?
该查询需要显示迄今为止制作最多电影的导演(名字,姓氏)。
答案 0 :(得分:0)
您需要在HAVING子句中提供逻辑条件,例如 最大(MD.movie_id)> 100
答案 1 :(得分:0)
ROW_NUMBER子句可以为您提供帮助。希望我没有任何错字。另外,如果您有一个person_id,我建议您使用该名称代替分区的姓氏。
WITH MaxMovies AS
(
SELECT P.lastname, P.firstname, ROW_NUMBER OVER (PARTITION BY firstname, lastname ORDER BY movie_id DESC) MOVIE_NUMBER
FROM Person AS P
inner join Movie_Directors AS MD ON P.person_id=MD.person_id
inner join Movie AS M ON M.movie_id=MD.movie_id
GROUP BY P.firstname, P.lastname
)
SELECT * FROM MAXMOVIES WHERE MOVIE_NUMBER=1
参考:https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql?view=sql-server-2017