首页 > 解决方案 > SQL where 列表中的元组

问题描述

我需要查询帮助,我需要为每年选择持续时间最长的课程名称。我的查询看起来像这样

select * 
from courses
where (cyear,duration) IN (
                          select cyear, max(duration)
                          from courses
                          group by cyear)

问题在于它会在以下位置出现以下错误:(cyear,

表达式 de type non booléen spécifiée dans un contexte où une condition est Attendue, près de ','。

标签: sqlsql-server

解决方案


SQL Server 不支持带有in. 最接近合理的语法是切换到相关子查询:

select c.* 
from courses c
where c.duration = (select max(c2.duration)
                    from courses c2
                    where c2.cyear = c.cyear
                   );

推荐阅读