如何选择最大值

时间:2013-03-14 10:44:08

标签: sql vb.net ms-access-2007

我有一个Access数据库,其中包含Grads表:

Grads
 - studentId 
 - courseId 
 - tryNumber
 - semesterId 
 - finalGrad

如何从最新studentName, courseName, courseId, finalGrade(最后一次尝试)的行中获取所有信息(tryNumber)?

4 个答案:

答案 0 :(得分:4)

如果你只想要一行带有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

答案 1 :(得分:0)

  

这应该适合你:

SELECT studentName, 
       courseName, couseId, 
       finalGrade, MAX(tryNumber) 
FROM grads
GROUP BY studentName, courseName, couseId, finalGrade

答案 2 :(得分:0)

您可以使用:

Select studentName, courseName, courseId, finalGrade From Grads
Where tryNumber=(Select MAX(tryNumber) From Grads)

答案 3 :(得分:-1)

select top 1 * from Grads order by tryNumber desc