Skip to content
Advertisement

Get consecutive count on repeated values with complex partition by in Postgres

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:

enter image description here

I have tried using

But none of the above get the expected results.

Thanks a lot for your help!

Advertisement

Answer

This is a type of gap and islands problem. The simplest method is probably to define the groups using the difference between a sequence per grade and semester. Then use row_number() on more time:

Here is a db<>fiddle.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement