I am trying to query a table that has a collection of student grades recorded over time. I would like to produce a result set that grabs the student id, the first grade, first grade date, last grade, last grade date.
I think I need to use the MIN
and MAX
functions and some subqueries to achieve this, but I'm just not getting the results I need.
Is there an effective way to achieve the results below on PostgreSQL?
Database Example:
user_id | grade | grade_date
1 | A | 01/05/2016
1 | B | 01/15/2016
1 | C | 01/31/2016
2 | A | 01/05/2016
2 | B | 01/15/2016
2 | C | 01/31/2016
3 | A | 01/05/2016
3 | B | 01/15/2016
3 | C | 01/31/2016
4 | A | 01/05/2016
4 | B | 01/15/2016
4 | C | 01/31/2016
Results I'm aiming for:
user_id | first_grade | first_date | last_grade | last_date
1 | A | 01/05/2016 | C | 01/31/2016
2 | A | 01/05/2016 | C | 01/31/2016
3 | A | 01/05/2016 | C | 01/31/2016
4 | A | 01/05/2016 | C | 01/31/2016