Oracle-加入条件不一样

时间:2017-05-05 07:34:16

标签: oracle join

我想加入一个select in select in select with table,我不必从表中的选择结果中找到customer_no。 另外,为了优化和缩短执行时间,我应该如何编写脚本?     离。

SELECT DISTINCT customer_no,
                str_data_expirare_1,
                data_expirare_buletin_1,
                str_data_expirare_2,
                data_expirare_buletin_2
  FROM (SELECT customer_no,
               str_data_expirare_1,
               data_expirare_buletin_1,
               str_data_expirare_2,
               data_expirare_buletin_2
          FROM (SELECT customer_no,
                       str_data_expirare_1,
                       normalize_date (str_data_expirare_trim_1)
                           AS data_expirare_buletin_1,
                       str_data_expirare_2,
                       normalize_date (str_data_expirare_trim_2)
                           AS data_expirare_buletin_2
                  FROM (SELECT customer_no,
                               str_data_expirare_1,
                               REGEXP_REPLACE (str_data_expirare_1,
                                               '[[:punct:]]',
                                               '')
                                   AS str_data_expirare_trim_1,
                               str_data_expirare_2,
                               REGEXP_REPLACE (str_data_expirare_2,
                                               '[[:punct:]]',
                                               '')
                                   AS str_data_expirare_trim_2
                          FROM (SELECT Q1.customer_no,
                                       Q1.set_act_id_1,
                                       NVL (SUBSTR (set_act_id_1,
                                                      INSTR (set_act_id_1,
                                                             '+',
                                                             1,
                                                             5)
                                                    + 1,
                                                    LENGTH (set_act_id_1)),
                                            'NULL')
                                           AS str_data_expirare_1,
                                       Q1.set_act_id_2,
                                       NVL (SUBSTR (set_act_id_2,
                                                      INSTR (set_act_id_2,
                                                             '+',
                                                             1,
                                                             5)
                                                    + 1,
                                                    LENGTH (set_act_id_2)),
                                            'NULL')
                                           AS str_data_expirare_2
                                  FROM STAGE_CORE.IFLEX_CUSTOMERS Q1
                                 WHERE Q1.set_act_id_1 IS NOT NULL/*AND Q1.PERS_LAST_NAME LIKE 'LACAN'*/
                                                                  )))
         WHERE     str_data_expirare_1 <> 'NULL'
               AND data_expirare_buletin_1 - SYSDATE < 0
               AND (   data_expirare_buletin_2 =
                           TO_DATE ('30-DEC-99', 'DD-MM-YYYY')
                    OR data_expirare_buletin_2 < SYSDATE)) T1
JOIN STAGE_CORE.IFLEX_CUSTOMERS_REPRES T2
           ON T1.CUSTOMER_NO = T2.representative
       JOIN STAGE_CORE.VW_IFLEX_ACCOUNTS_IBAN t3
           ON t3.Cust_key <> T2.representative
 WHERE T2.relationship <> 'EXTERN' AND T2.REPRESENTATIVE <> T2.REF_NO

查询必须查找所有身份证已过期且已在其他人帐户(来自银行)授权但未开立任何帐户的人。

  • STAGE_CORE.IFLEX_CUSTOMERS_REPRES - 包含所有帐户的表格 通过帐户授权
  • JOIN STAGE_CORE.VW_IFLEX_ACCOUNTS_IBAN - 包含所有帐户的表格 和细节
  • STAGE_CORE.IFLEX_CUSTOMERS - 包含所有客户的表格

样本数据:

  • VW_IFLEX_ACCOUNTS_IBAN

    UNIT_ID CUST_KEY ACCOUNT_NO         IBAN                AC_OPEN_DATE ACC_ISOPEN
      51    875497  xxxxxxxxxxxxxxxxxx  xxxxxxxxxxxxxxxxxx   29/02/2016   O
      51    875497  xxxxxxxxxxxxxxxxxx  xxxxxxxxxxxxxxxxxx   08/03/2016   C
      51    875497  xxxxxxxxxxxxxxxxxx  xxxxxxxxxxxxxxxxxx   01/03/2016   O
     751    875497  xxxxxxxxxxxxxxxxxx  xxxxxxxxxxxxxxxxxx   12/08/2011   C
     466    875497  xxxxxxxxxxxxxxxxxx  xxxxxxxxxxxxxxxxxx   22/01/2013   O
    
  • IFLEX_CUSTOMERS_REPRES

    REF_NO CATEGORY TITULAR REPRESENTATIVE RELATIONSHIP
    875497     C     875497    535555      UNICA
    875497     C     875497    565796      UNICA
    875497     C     875497    875497      PRIMARY-MEANS I AM AUTHORIZED ON MY ACCOUNT
    875497     C     875497    875497      UNICA-MEANS I AM AUTHORIZED ON MY ACCOUNT
    875497     C     875497    0O00000     EXTERN -- MEANS EVERYONE CAN PUT MONEY ON MY ACCOUNT
    
  • IFLEX_CUSTOMERS

    CUSTOMER_NO LEGACY_CUSTOMER_NUMBER_KEY UNIT_ID CUST_CATEGORY CUST_CLASSIFICATION CUST_TYPE KYC_CUST_TYPE CIF_SINCE FISCAL_ID    FULL_NAME         FISCAL_RESIDENCE_COUNTRY NIF_EXPIRED_DATE NIF                   SET_ACT_ID_1               SET_ACT_ID_2
    875497      875497                     xxx     RETAIL         80                    I              R     254911    2548614654 LACAN MARIA ADRIANA              RO              01/03/2016   CI+XX+8646844+SPCLEP SIMM+01-Oct-2015+06-Dec-2022 +FN+FN+++
    

输出:

CUSTOMER_NO STR_DATA_EXPIRARE_1 DATA_EXPIRARE_BULETIN_1 STR_DATA_EXPIRARE_2 DATA_EXPIRARE_BULETIN_2
535555     29-Apr-16           29-Apr-16               NULL                30-Dec-99
0Jxx1ds0    09-Sep-16           09-Sep-16               NULL                30-Dec-99
2xx8ds      24-Mar-16           24-Mar-16               NULL                30-Dec-99
2756719     20-Feb-17           20-Feb-17               NULL                30-Dec-99

此客户不得拥有该银行的任何帐户

2 个答案:

答案 0 :(得分:0)

坦率地说,我将忽略那些庞大的代码并使用您应该发布的重点突出的可重复测试案例回答问题:)

您需要的是反加入。使用left join语法将Customers与Accounts连接,然后过滤Account键为null的结果。这为您的客户提供了无帐户。但是inner join关于客户和代表,以确保客户集仅限于那些也是代表的客户。

select c.customer_no
       , c.expired_date
from customers c
     inner join customers_repres cr
          on cr.representative_id =  c.customer_no
     left outer join accounts_iban ai
          on ai.cust_key =  c.customer_no
where c.expired_date is not null -- ?? indicates expired ID ??
and ai.cust_key is null

答案 1 :(得分:0)

解决:

select * from STAGE_CORE.IFLEX_CUSTOMERS c
join
STAGE_CORE.IFLEX_CUSTOMERS_REPRES cr
on c.CUSTOMER_NO=cr.REPRESENTATIVE
left join STAGE_CORE.VW_IFLEX_ACCOUNTS_IBAN a
on a.cust_key=cr.REPRESENTATIVE
where cr.RELATIONSHIP<>'PRIMARY'
and a.CUST_KEY is not null