使用内部联接

时间:2017-06-20 18:57:16

标签: sql oracle11g inner-join

以下是oracle数据库的两个表(11g):

表1:EXPORT_TABLE

Insert into EXPORT_TABLE (INTBA,INDEXNAME,BANAME,SCBA,NAME,NVRCOUNTRY) values ('133520', 'Hong Kong',659923,'0',' Trustee Limited','HK');

Insert into EXPORT_TABLE (INTBA,INDEXNAME,BANAME,SCBA,NAME,NVRCOUNTRY) values ('133516',' Holdings ',659923,'0',' Banking Holdings','CH');

表2:ba_name

Insert into ba_name (MAINKEY,INTBA,CLIENTNAME,BASEQ,TRAN_DATE,name_type_code,indx_name_e_dte) values (1000,'133520','ROYAL Challenge', 0, '2016-MAR-10' ,'001',NULL);

Insert into ba_name (MAINKEY,INTBA,CLIENTNAME,BASEQ,TRAN_DATE,name_type_code,indx_name_e_dte) values (1001,'133520','Royal Challenge',0, '2016-SEPT-24','001',NULL);

Insert into ba_name (MAINKEY,INTBA,CLIENTNAME,BASEQ,TRAN_DATE,name_type_code,indx_name_e_dte) values (1003, '133516','Deloitte AG','0','2016-MAY-20','001',NULL);

要求需要在下面提到的连接条件下将EXPORT_TABLE与ba_name连接

          ba_name.INTBA = EXPORT_TABLE.INTBA
          AND ba_name.name_type_code = '0001'
          AND ba_name.indx_name_e_dte IS NULL

在第二张表格中' ba_name' ,INTBA不是唯一的密钥,第二个表是" ba_name'可以有多行相同的INTBA,可以在不同的交易日期发生。它是一个遗留系统(现在任何东西)

由于我们有重复的INTBA条目,我们需要将EXPORT_TABLE(列名= INTBA)与ba_name表的行连接,该表具有最新的TRAN_DATE。

换句话说,我想加入EXPORT_TABLE和ba_name上的匹配记录,该记录是最近的或具有最新的TRAN_DATE日期。我想加入INTBA' 133520'与INTBA' 133520'出口有关的出口情况具有tran_date的ba_name为' 2016-SEPT-24' ,而不是具有MARCH作为tran_date的那个(' 2016-MAR-10')。

THE RESULT SET WANTED TO SEE IS :


                             (intBA |CLIENTNAME| BASEQ | TRAN_DATE)
                             ======================================

                             133520 |'Royal Challenge'| 0  | '2016-SEPT-24'
                             133516 |'Deloitte AG'    |'0' | '2016-MAY-20'

但尝试使用以下查询:

SELECT   EXPORT_TABLE.intBA, 
         ba_name.CLIENTNAME,
         ba_name.BASEQ,             
         ba_name.TRAN_DATE

         FROM EXPORT_TABLE   INNER JOIN  ba_name

       ON ba_name.INTBA = EXPORT_TABLE.INTBA

              AND ba_name.name_type_code = '0001'
              AND ba_name.indx_name_e_dte IS NULL

I got below result which is incorrect:


                             (intBA |CLIENTNAME| BASEQ | TRAN_DATE)
                             ======================================

                             133520 |'Royal Challenge'| 0  | '2016-SEPT-24'
                             133520 |'RoYAL Challenge'| 0  | '2016-MAR-10'
                             133516 |'Deloitte AG'    | 0 | '2016-MAY-20'

您能否建议如何使用具有最新交易日期的第二(右)表的匹配行加入左侧表。

2 个答案:

答案 0 :(得分:0)

你可能正在寻找类似的东西: 添加的subselect返回每个组合的最新TRAN_DATE

@Repository
public interface LocationRepository extends JpaRepository<Location, 
Long> {)

答案 1 :(得分:0)

一种常见的方法是row_number()

SELECT e.intBA, n.CLIENTNAME, n.BASEQ, n.TRAN_DATE
FROM EXPORT_TABLE e INNER JOIN
     (SELECT n.*,
             ROW_NUMBER() OVER (PARTITION BY n.INTBA ORDER BY n.TRAN_DATE DESC) as seqnum
      FROM ba_name n
      WHERE n.name_type_code = '0001' AND n.indx_name_e_dte IS NULL
     ) n
     ON n.INTBA = e.INTBA AND n.seqnum = 1;