С NOT EXISTS
:
select cr.* from coltures_report cr
where not exists (
select 1 from coltures_report
where application_id = cr.application_id and row_step > cr.row_step
)
или с rank()
оконной функцией:
select cr.id, cr.application_id, cr.coltures_id, cr.row_step
from (
select *,
rank() over (partition by application_id order by row_step desc) rn
from coltures_report
) cr
where cr.rn = 1
Или с коррелированным подзапросом:
select cr.* from coltures_report cr
where cr.row_step = (select max(row_step) from coltures_report where application_id = cr.application_id)
См. демо . Результаты:
> id | application_id | coltures_id | row_step
> -: | -------------: | ----------: | -------:
> 1 | 1169 | 4 | 5
> 2 | 1169 | 5 | 5
> 4 | 1124 | 1 | 5
> 6 | 1156 | 1 | 5
> 7 | 1156 | 2 | 5
> 8 | 1156 | 3 | 5