1

I need to bring the result of a consultation.

I need to bring the latest version of each inst_code, as well as its name (table B)

My current scenario

2 Answers 2

1

In Postgres, you can use distinct onto solve this top-1-per-group problem:

select distinct on(a.inst_code)
    a.inst_code,
    b.inst_name,
    a.version,
    a.status,
    a.date
from tablea a
inner join table b on b.inst_code = a.inst_code
order by a.inst_code, a.version desc, a.date desc
Sign up to request clarification or add additional context in comments.

Comments

1

I think you can use distinct on:

select distinct on (inst_code) *
from b join
     a
     using (inst_code)
order by inst_code, version desc;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.