我有下表。
我需要选择SemesterID,AcadamiYear,2015年最高学年和学术学期的学术学期
预期输出
2013 1 2
我尝试了以下查询,但它返回了两个记录
select MAX(AcadamiYear) as Year,
MAX(AcadamicSemester) as Semester
,SemesterID
from
tblSemesterRegistration
where [IntakeYear]='2015'
Group by SemesterID
答案 0 :(得分:4)
由于您正在搜索单个记录,因此您可以使用TOP 1,按您的意图排序
select TOP 1 *
from
tblSemesterRegistration
where [IntakeYear]='2015'
Order by AcadamiYear DESC, AcadamicSemester DESC
答案 1 :(得分:1)
这是您正在寻找的查询:
SELECT SR.*
FROM tblSemesterRegistration SR
INNER JOIN (SELECT MAX(SR2.AcadamiYear) AS [AcadamiYear]
,MAX(SR2.AcadamicSemester) AS [AcadamicSemester]
,IntakeYear
FROM tblSemesterRegistration SR2
GROUP BY SR2.IntageYear) T ON T.AcadamiYear = SR.AcadamiYear
AND T.AcadamicSemester = SR.AcadamicSemester
AND T.IntakeYear = SR.IntakeYear
WHERE SR.IntakeYear = '2015'
希望这会对你有所帮助。
答案 2 :(得分:0)
SELECT MAX(AcadamiYear) AS Year,
MAX(AcademicSemester) AS Semester,
MAX(SemesterID) AS SemesterID
FROM tblSemesterRegistration
WHERE [IntakeYear] = '2015'
GROUP BY IntakeYear
答案 3 :(得分:0)
如果SemesterID
是主键,则对其进行分组将始终产生所有行(因为它始终是唯一的)。
我猜你的意思是找回参数设置的那个学期id:
select r.*
from tblSemesterRegistration r
join ( select max(AcadamiYear) as Year
, max(AcadamicSemester) as Semester
from tblSemesterRegistration
where [IntakeYear]='2015'
) m
on r.acadamiyear = m.year
and r.acadamicsemester = m.semester