Versioning entities in db with draft option, select query
You can use the following, which relies on DISTINCT ON, and turns calculates version -1 as 9999999:

select DISTINCT ON (field_id) *
from field_mappings 
where COALESCE(NULLIF(version, -1), 9999999) &lt; 4
ORDER BY field_id, COALESCE(NULLIF(version, -1), 9999999) DESC


The query retrieves all the rows with unique fields (by field_id), prioritising the version you specify, or the draft one (which has its priority bumped to the top). If you want the draft or last version, then use &lt; 10000000 instead of &lt; 4.