Nie znam ColdFusion, ale chyba zrozumiałem logikę. Priorytet jest aktualny> historia> inny. Nie jest jasne, który wiersz jest poprawny, gdy są dwa current wiersze lub tylko different wiersze, więc zaznaczam wiersz z minimalnym valid_from W takim wypadku. Jeśli cię to nie obchodzi Możesz pominąćten parametr (usuńunit_valid_from z row_number order by klauzula):
select units.*,
case when 1 =
row_number() over (
partition by org_id
order by case origin when 'current' then 1 when 'history' then 2 else 3 end,
unit_valid_from ) then 1 else 0 end as is_correct_version
from units