Q:

In PostgreSQL, how can I get the máximum value for which all previous values satisfy a condition

I have a PostgreSQL table which records the test results of different students:
Student  Exam Passed
A        1    True
A        2    True
A        3    False
A        4    True
B        1    False
B        2    True

What I'd like to get is the maximum exam value for which all previous exams values are true. For example, in case of student A it would be 2 (not 4, because value un exam 3 is true) and for student B it would be zero, given that level 1 is false
Student     Maximum level
A           2
B           0

Any help? Thanks!

A:

Group by student and find the first exam with Passed = False:
select
  student,
  coalesce(min(case when not passed then exam end) - 1, max(exam)) as "Maximum level"
from tablename
group by student
order by student

See the demo.
Results:
| student | Maximum level |
| ------- | ------------- |
| A       | 2             |
| B       | 0             |

