oracle SQL连接未返回正确的数据

1wnzp6jl  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(126)

我正在尝试用SQL构建一些测试用例。问题是,当我试图用左连接(如果我的理解是正确的,应该返回匹配和不匹配的行)连接一些表时,当我试图获取没有匹配ID的数据时,没有数据返回。
代码如下:

select
      E.ID_EVENEMENT,
      t1.dateRecente as date_Star,
      virage_c.no_contr,
      t2.id as id_Univers,
      t2.dateRecente as date_Univers,
      t3.NUMERO_DOSSIER_STAR,
      t3.dateRecente as Date_factcan,
      t3.dateLimite,
      t2.ID_PERSONNE_UNIVERS,
      t1.ID_PERSONNE_STAR
   FROM
      STAR.EVENEMENT e
         left join VIRAGE.CONTRAT virage_c 
            on e.NO_CONTRAT_OFFICIEL = to_char(virage_c.no_contr)
         inner join 
         ( SELECT
                 e.ID_EVENEMENT,
                 ep.ID_EVEN_INDIVIDU as ID_PERSONNE_STAR,
                 GREATEST( e.dt_creation,
                           NVL(e.DT_MODIF_STA_ELI, TO_DATE(1,'j')),
                           NVL(max(nt.dt_maj), TO_DATE(1,'j')),
                           NVL(max(ser.DT_CREATION), TO_DATE(1,'j')),
                           NVL(max(SER.DT_MAJ), TO_DATE(1,'j')),
                           NVL(max(aut.dt_transmis), TO_DATE(1,'j')),
                           NVL(max(AUT.DT_CREATION), TO_DATE(1,'j')) ) dateRecente
              FROM 
                 STAR.EVENEMENT e
                    left join STAR.Note nt 
                       on e.ID_EVENEMENT = nt.ID_EVEN
                    left join STAR.SERVICE ser 
                       on e.ID_EVENEMENT = ser.ID_EVEN
                       left join STAR.AUTORISATION aut 
                          on ser.id_service = aut.id_service
                    left join STAR.DOCUMENT doc 
                       on e.ID_EVENEMENT = doc.ID_EVENEMENT
                    left join STAR.ETAT ett 
                       on e.ID_EVENEMENT = ett.ID_EVENEMENT
                    left join STAR.EVENEMENT_PARTICIPANT ep 
                       on e.ID_EVENEMENT = ep.ID_EVENEMENT
              GROUP BY 
                 e.ID_EVENEMENT,
                 e.dt_creation,
                 e.DT_MODIF_STA_ELI,
                 ep.ID_EVEN_INDIVIDU ) t1 
                 on t1.ID_EVENEMENT = E.ID_EVENEMENT
         left JOIN 
         ( SELECT
                 sf.STAREVENTNUMBER,
                 c.id,
                 par.ID as ID_PERSONNE_UNIVERS,
                 GREATEST( c.UPDATEDATE,
                           max(NVL(ca.UPDATEDATE, TO_DATE(1, 'J'))),
                           max(NVL(bo.UPDATEDATE, TO_DATE(1, 'J'))),
                           max(NVL(a.UPDATEDATE, TO_DATE(1, 'J'))),
                           max(NVL(p.UPDATEDATE, TO_DATE(1, 'J'))),
                           max(NVL(p.RELEASEDATE, TO_DATE(1, 'J'))),
                           max(NVL(p.PAYMENTDATE, TO_DATE(1, 'J'))) ) dateRecente
              FROM
                 CV_CLAIMS_TRAVEL.STAR_FILE sf
                    join CV_CLAIMS_TRAVEL.CLAIM c 
                       on sf.claimid = c.id
                       left join CV_CLAIMS_TRAVEL.BENEFIT_OPTION bo 
                          on c.id = BO.CLAIMID
                          left join CV_CLAIMS_TRAVEL.ADJUDICATION a 
                             on bo.id = a.BENEFITOPTIONID     
                       left join CV_CLAIMS_TRAVEL.CLAIM_ACTIVITY ca 
                          on c.id = CA.CLAIMID
                       left join CV_CLAIMS_TRAVEL.CLAIM_RELATIONSHIP cr 
                          on c.id = CR.CLAIMID
                          left join CV_CLAIMS_TRAVEL.PAYEE pa 
                             on cr.id = PA.CLAIMRELATIONSHIPID
                             left join CV_CLAIMS_TRAVEL.PAYMENT p 
                                on pa.id = P.PAYEEID
                    left join CV_CLAIMS_TRAVEL.PARTY par 
                       on par.ID = sf.PARTYID
              WHERE
                     c.PRIMARYSTATUSLID in ('CLAIM_PRIMARY_STATUS:0000000003','CLAIM_PRIMARY_STATUS:0000000001') 
                 OR (   c.PRIMARYSTATUSLID = 'CLAIM_PRIMARY_STATUS:0000000004' 
                    AND bo.BENEFITOPTIONSTATUSLID in ('BENEFIT_OPTION_STATUS:0000000010', 
                                                      'BENEFIT_OPTION_STATUS:0000000060', 
                                                      'BENEFIT_OPTION_STATUS:0000000030')
                    )
              group by 
                 sf.STAREVENTNUMBER, 
                 c.id, 
                 c.UPDATEDATE, 
                 par.ID ) t2 
            on e.ID_EVENEMENT = t2.STAREVENTNUMBER
         LEFT JOIN 
         ( SELECT DISTINCT
                 fact.NUMERO_DOSSIER_STAR,
                 GREATEST( to_date(fact.VSTDTCHG,'yyyymmdd'),
                           to_date(fact.VSTDICHK,'yyyymmdd'),
                           to_date(decode(fact.VSTDIFIN,0,19000101,
                                decode(substr(fact.VSTDIFIN,5),
                                '0230', substr(fact.VSTDIFIN,1,4) 
                                || '0301',fact.VSTDIFIN)),'yyyymmdd') ) DateRecente,
                 DECODE( virage_cont.id_cont,
                         null,add_months(sysdate,-7*12),
                         add_months(sysdate,-15*12)) dateLimite
              FROM
                 FACTCAN.XC4DSAV fact
                    LEFT JOIN VIRAGE.CONTRAT virage_cont 
                       on fact.VSTNOCNT_VIRAGE = virage_cont.NO_CONTR
              where 
                 fact.NUMERO_DOSSIER_STAR is not null ) t3 
            on e.ID_EVENEMENT = t3.NUMERO_DOSSIER_STAR
   WHERE
          t1.dateRecente < add_months(sysdate, -7*12) 
      AND t2.dateRecente < add_months(sysdate, -7*12) 
      AND virage_c.id_cont is null 
      AND t2.ID_PERSONNE_UNIVERS is not null 
      AND t1.ID_PERSONNE_STAR is null 
      AND t3.dateRecente < t3.dateLimite
   FETCH 
      FIRST 1000 ROWS ONLY;

当我试图从t1.ID_PERSONNE_STAR或t2.ID_PERSONNE_UNIVERS为null的事件中获取结果时,查询实际上应该返回一些数据,但却没有返回任何内容。但是,不是null可以按预期工作。有什么想法吗?

vi4fp9gy

vi4fp9gy1#

我更新了查询的可读性。另外,正如其他人所指出的,你必须小心WHERE子句。你可能会遇到的问题是你的where子句为T1,T2和T3的左连接。把这些移到连接处... ex
在那里你有

left join (rest of the left-join subquery with alias ) t1 
     on t1.ID_EVENEMENT = E.ID_EVENEMENT

改变

on t1.ID_EVENEMENT = E.ID_EVENEMENT
 AND t1.dateRecente < add_months(sysdate, -7*12)

这样,日期要求是LEFT-JOIN的一部分。通过在WHERE子句中使用that,将其转换为INNER JOIN。
您可以在where子句部分保留T1“IS NULL”测试,因为您显式地期望没有匹配的记录。
检查类似的情况,为您的其他左连接.将CRITERIA移到JOIN/ON子句中,并从WHERE中删除。where子句应该有“IS NULL”作为最终期望。

相关问题