使用oracle

时间:2018-11-01 10:37:36

标签: sql oracle

我对联接表有疑问。我有三张桌子。在表2中,相同的ID有不同的值,我需要根据较高的值检索行。请帮助我。这是一个新概念。到目前为止,我已经尝试过

select a.trefno, a.tstatusno, max(b.tstatusno), b.trefno, distinct c.trefno, c.status
from Table1 a
inner join Table3 c on a.trefno = c.TREFNO
inner join Table2 b on c.trefno = b.TREFNO
where b.tstatusno = (select max(tstatsno) from Table2 t where b.tref = t.trefno group by t.trefno)
group by a.trefno

这是我展示的三个表格示例

X

我想用tref将所有三个表作为唯一ID和表2中的max(tstatusno)联接起来。

X

1 个答案:

答案 0 :(得分:1)

您可以使用窗口功能:

select a.trefno, a.tstatusno, b.tstatusno, b.trefno, c.trefno, c.status
from Table1 a inner join
     Table3 c
     on a.trefno = c.TREFNO inner join
     (select b.*,
             row_number() over (partition by b.trefno order by b.statusno desc) as seqnum
      from Table2 b
     ) b 
     on c.trefno = b.TREFNO and b.seqnum = 1