how to select the max value with a criteria

(
    SELECT
        sd1.student_id, sd1.student_startdate, sd1.student_enddate
    FROM (
        SELECT student_id, MAX(student_enddate) AS enddate
        FROM studentdates
        GROUP BY student_id
    ) AS s_max_enddate
    JOIN studentdates AS sd1 ON (
        sd1.student_id = s_max_enddate.student_id
        AND sd1.student_enddate = s_max_enddate.enddate
    )
)

评论

热门博文