Joining tables based on the maximum value

后端 未结 6 1701
暖寄归人
暖寄归人 2021-02-08 22:00

Here\'s a simplified example of what I\'m talking about:

Table: students      exam_results
_____________       ____________________________________
| id | name |         


        
6条回答
  •  隐瞒了意图╮
    2021-02-08 22:47

    With Oracle's analytic functions this is easy:

    SELECT DISTINCT
           students.name
          ,FIRST_VALUE(exam_results.score)
           OVER (PARTITION BY students.id
                 ORDER BY exam_results.score DESC) AS score
          ,FIRST_VALUE(exam_results.date)
           OVER (PARTITION BY students.id
                 ORDER BY exam_results.score DESC) AS date
    FROM   students, exam_results
    WHERE  students.id = exam_results.student_id;
    

提交回复
热议问题