0
我的設立是這樣的:我有三個表 -SQL的子查詢與聚合函數(SQL Server)的
- 學生(StudentID,名字,姓氏等)
- 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