我的设置是这样的:我有三张桌子-
- 学生(StudentID、FirstName、LastName 等),
- StudentSemesters(StudentID、SemID 等),以及
- 学期(SemID,年)
我的要求是获取每个学生的详细信息,但仅限于他们最后一个学期的详细信息。从逻辑上讲,这意味着年数最高的学期。我似乎无法正确查询。为简单起见,“年份”只是一个整数(例如 2000、1998)。以下是我一直坚持的当前查询:
SELECT dbo.Student.LastName + ' , ' + dbo.Student.FirstName AS Student, dbo.Student.Defence1Date, dbo.Student.Defence2Date, COUNT(StudentSemesters_1.SemID)
AS SemesterCount, dbo.Student.EntrySemester + ' - ' +
(SELECT dbo.StudentSemesters.SemID
FROM dbo.StudentSemesters INNER JOIN
dbo.ListSemesters ON dbo.StudentSemesters.SemID = dbo.ListSemesters.SemID
WHERE (dbo.Student.StudentCode = dbo.StudentSemesters.StudentCode)
GROUP BY dbo.StudentSemesters.SemID, dbo.ListSemesters.Year
HAVING (dbo.ListSemesters.Year = MAX(dbo.ListSemesters.Year))) AS Expr1
FROM dbo.Student INNER JOIN
dbo.StudentSemesters AS StudentSemesters_1 ON dbo.Student.StudentCode = StudentSemesters_1.StudentCode
GROUP BY dbo.Student.LastName, dbo.Student.FirstName, dbo.Student.Defence1Date, dbo.Student.Defence2Date, dbo.Student.EntrySemester,
dbo.Student.StudentCode