简体   繁体   English

使用oracle在一个表中基于最大值联接三个表

[英]Joining three tables based on max value in one table using oracle

I have a question for joining tables. 我有一个关于联接表的问题。 I have three tables. 我有三张桌子。 In table 2 there are different value to same ID and i need to retrieve row based on higher value. 在表2中,相同的ID有不同的值,我需要根据较高的值检索行。 Kindly help me. 请帮助我。 Am new to this concept. 这是一个新概念。 Till now I have tried 直到现在我已经尝试过

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

These is the three table samples i have shown 这是我展示的三个表格示例

X

I want to join all three table with tref as unique id and max(tstatusno) from table 2. 我想将所有三个带有tref的表作为唯一ID和表2中的max(tstatusno)连接起来。

X

You can use window functions: 您可以使用窗口功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM