Given a set of 3 columns (blue columns: student, semester, grade) I need to calculate for how many consecutive semesters has each student had the same grade (green column) using a query in Postgres.
(first consecutive semester with avg 8: consec = 1. second consec semester with avg 8: consec = 2...)
Sample data fiddle: https://www.db-fiddle.com/f/v31a5Bpere26tXQb5L1fFJ/0
Expected result:
I have tried using
ROW_NUMBER() OVER(partition by student)
ROW_NUMBER() OVER(partition by avg_grade)
ROW_NUMBER() OVER(partition by student, avg_grade)
ROW_NUMBER() OVER(partition by student, semester)
But none of the above get the expected results.
Thanks a lot for your help!