分两次查询 Postgres table

Query Postgres in two table

我要带咨询结果

我需要带上每个 inst_code 的最新版本,以及它的名字 (table B)

在 Postgres 中,您可以使用 distinct on 来解决这个 top-1-per-group 问题:

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

我想你可以使用 distinct on:

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