选择语句,其中联接导致记录被排除

时间:2019-10-23 08:21:48

标签: sql sql-server

我在下面的查询中有多个联接。最后3个联接是获取g.fin_id值所必需的。但是,这样做很好(请参见结果),因为ACCUM_ISS_CHAR_HIST表中的某些记录的e.char9_nme值为NULL,因此完全排除了结果中的记录。这样看来,当e.char9_nme值具有记录时,它将产生结果,但是一旦它具有Null值,就将其排除在外。我仍然希望看到记录,即使这些记录的g.fin_id将为空,因为它们的e.char9_nme值为Null。如何更改查询以完成此操作?

results here

select 
      a.acct_id, 
      c.fld3_txt, 
      b.issue_loc1_cde,
      b.instr_id, 
      a.fld1_nme, 
      b.issue_cls2_nme, 
      g.fin_id, 
      e.char9_nme 

from position_dg as a
     inner join 
     infoportal..issue_dg as b on b.INSTR_ID = a.INSTR_ID
     inner join 
     InfoPortal..IVW_ACCT as c on a.acct_id = c.acct_id
     inner join 
     InfoPortal..DW_AcctCharDG as d on a.acct_id = d.acctid
     inner join 
     ACCUM_ISS_CHAR_HIST as e on a.instr_id = e.instr_id
     inner join 
     MD_FINANCIAL_ENTITY as f on e.char9_nme = f.fin_enty_name 
     inner join md_FINANCIAL_ENTITY_ALTERNATE_IDENTIFIER as g on 
                f.fin_enty_id = g.fin_enty_id
                and b.MAT_EXP_DTE > getdate()
                and b.issue_cls1_nme = 'Derivatives'
                and a.as_of_tms >= getdate()-1
                and b.iss_typ in ('FFX','IRS','EQF')
                and d.AcctChrSetId = 'DerivativeRpt'
                and d.EndTms IS NULL
                and a.acct_id = 'FOGEMBLCR'
                and g.id_ctxt_typ = 'LEGAL_ENTITY_IDENTIFIER'
                and e.as_of_dte = (
                                   select MAX (as_of_dte)-1 
                                   from accum_iss_char_hist
                                  )

我希望结果会显示一些fin_id记录,而某些显示空白fin_id记录,但是目前只有那些带有fin_id记录的记录才行,其余的将从结果中排除。

1 个答案:

答案 0 :(得分:0)

您正在寻找左联接。

加入所有这些表(如您所说的最后3个)作为左连接。为了更好的说明,我将每个表的条件移到了它们的ON子句中,并将基本表a的where子句中移了。

select 
      a.acct_id, 
      c.fld3_txt, 
      b.issue_loc1_cde, 
      b.instr_id, 
      a.fld1_nme, 
      b.issue_cls2_nme, 
      g.fin_id, 
      e.char9_nme 

from  position_dg as a
      inner join
      infoportal..issue_dg as b on b.INSTR_ID = a.INSTR_ID 
                                   and b.MAT_EXP_DTE > getdate()
                                   and b.issue_cls1_nme = 'Derivatives'
                                   and b.iss_typ in ('FFX','IRS','EQF')
      inner join 
      InfoPortal..IVW_ACCT as c on a.acct_id = c.acct_id
      inner join 
      InfoPortal..DW_AcctCharDG as d on a.acct_id = d.acctid
                                     and d.AcctChrSetId = 'DerivativeRpt'
                                     and d.EndTms IS NULL
      left join 
      ACCUM_ISS_CHAR_HIST as e on a.instr_id = e.instr_id 
                               and e.as_of_dte = (
                                         select MAX (as_of_dte)-1 
                                         from accum_iss_char_hist
                                                  )
      left join 
      MD_FINANCIAL_ENTITY as f on e.char9_nme = f.fin_enty_name 
      left join 
      md_FINANCIAL_ENTITY_ALTERNATE_IDENTIFIER as g on f.fin_enty_id = g.fin_enty_id 
                                          and g.id_ctxt_typ = 'LEGAL_ENTITY_IDENTIFIER'
Where a.as_of_tms >= getdate()-1
      and a.acct_id = 'FOGEMBLCR'