如何检索MAX查询的整行?

时间:2013-03-29 08:30:59

标签: sql database

TABLE.PNG

我目前正在尝试使用此查询来检索突出显示的行

“从2012年和2013年之间的等级选择max(grades_id)WHET dt_year BETWEEN 2012和2013 GROUP BY student_id HAVING count(grades_id)> = 2”

然而它只是屈服

RESULTS.PNG

有没有办法检索整行而不仅仅是grades_id?

2 个答案:

答案 0 :(得分:1)

只需将现有查询用作子查询即可确定所需的行,并为其选择所有列,如下所示:

select * from grades where grades_id in (Select max(grades_id) from grades WHERE dt_year BETWEEN 2012 AND 2013 GROUP BY student_id HAVING count(grades_id) >= 2)

答案 1 :(得分:0)

查询:

SELECT g.*
FROM grades g
WHERE g.grades_id = (SELECT MAX(g1.grades_id)
                     FROM grades g1
                     WHERE g1.dt_year BETWEEN 2012 AND 2013
                     AND g1.student_id = g.student_id
                     HAVING count(g1.grades_id) >= 2)

如果您可以在http://sqlfiddle.com/中插入数据,我可以提供更优化的解决方案。