0

TABLE.PNG

Im currently using this query in attempts to retrieve the highlighted rows

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

however it is only yielding

RESULTS.PNG

is there any way to retrieve the entire row as opposed to just the grades_id?

4

2 回答 2

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)
于 2013-03-29T08:49:01.520 回答
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/中插入数据,也许我可以给出更优化的解决方案。

于 2013-03-29T08:48:31.737 回答