У меня есть эта таблица профиль :
Я хочу извлечь строки для профилей, которые имеют form=final
и являются самыми последними (year=max
) для отдельных стран, то есть результаты запросов будут:
и т. д.
Есть ли способпреобразовать один из следующих запросов в формат JPAQuery или querydsl? (первый относится к Postgres)
Select distinct on (p.country) * from profile p where p.form='final' order by p.country, p.year desc;
или
SELECT p.id, p.country, p.version, p.year from profile p inner join (SELECT DISTINCT country as c, max(year) as y from profile where form='final' group by country) as pro on pro.c=p.country and pro.y=p.year where form='final';