我有一个Access
包含Grads
表的数据库:
Grads
- studentId
- courseId
- tryNumber
- semesterId
- finalGrad
如何studentName, courseName, courseId, finalGrade
从 max tryNumber
(last try) 行获取所有信息 () ?
我有一个Access
包含Grads
表的数据库:
Grads
- studentId
- courseId
- tryNumber
- semesterId
- finalGrad
如何studentName, courseName, courseId, finalGrade
从 max tryNumber
(last try) 行获取所有信息 () ?
如果您只想要带有max
trynumber 的一行,那么您应该可以使用:
select g.studentid,
g.courseid,
g.trynumber,
g.semesterid,
g.finalgrade
from grads g
inner join
(
select max(tryNumber) MaxTry
from grads
) m
on g.trynumber = m.maxtry
如果您想返回每个学生max(tryNumber)
,那么您可以使用:
select g.studentid,
g.courseid,
g.trynumber,
g.semesterid,
g.finalgrad
from grads g
inner join
(
select studentid, max(tryNumber) MaxTry
from grads
group by studentid
) m
on g.trynumber = m.maxtry
and g.studentid = m.studentid
这应该适合你:
SELECT studentName,
courseName, couseId,
finalGrade, MAX(tryNumber)
FROM grads
GROUP BY studentName, courseName, couseId, finalGrade
您可以使用:
Select studentName, courseName, courseId, finalGrade From Grads
Where tryNumber=(Select MAX(tryNumber) From Grads)
select top 1 * from Grads order by tryNumber desc