我在查询中需要帮助,我需要为每年选择具有最长持续时间的课程名称。 我的查询看起来像这样
select *
from courses
where (cyear,duration) IN (
select cyear, max(duration)
from courses
group by cyear)
问题在于:(cyear,
表达de typenonbooléenspécifiéedansuncontexteoùunecondition est attendant,prèsde','。
答案 0 :(得分:1)
SQL Server不支持in
的元组。最合理的语法是切换到相关子查询:
select c.*
from courses c
where c.duration = (select max(c2.duration)
from courses c2
where c2.cyear = c.cyear
);
答案 1 :(得分:1)
使用Windows功能
select *
from ( select *
, DENSE_RANK( ) over (partition by cyear order by duration desc) as rn
) tt
where tt.rn = 1
答案 2 :(得分:0)
或者,您可以选择EXISTS
:
SELECT *
FROM courses c
WHERE EXISTS (SELECT 1
FROM cources e
WHERE e.cyear = c.cyear
HAVING MAX(e.duration) = c.duration);