如何使用左连接而不是内连接

时间:2017-02-21 20:28:43

标签: sql-server join

我编写了以下查询,该表连接已连接表的表。我基本上使用SQL Server进行内连接。我想知道如何修改下面的代码使用左连接而不是内连接?

以下查询有三个表Blended,BOUND_TAB和RECORD

第二件事是,我还需要过滤结果,以便我使用带有deductibleinUSD>的策略。 0'混合' table或Primary_R Type =' Deductible'和Primary_R金额>内部选择0。这应该是我在最终结果中的政策,如果其中任何一个'混合'或者' BOUND_TAB'表示它有免赔额。

select
    c.MPolicyNumber,
    c.SNumber,
    c.InsuredName,
    c.EffDate,
    c.Renewal,
    c.GPremiumUSD,
    c.Status,
    c.deductibleinUSD, t.*
from 
    IT.dbo.Blended c 
inner join
    (select distinct 
         a.[Policy Number], a.[LOB],
         a.[Primary_R Amount] as Bound_deductibles,
         a.[Primary_R Type],
         a.[EffDate] as CAS_EffDate
     from 
         IT.dbo.BOUND_TAB a
     inner join 
         IT.dbo.RECORD b on a.idxFile = b.[Bound Rater]
     where 
         a.[Primary Retention Amount] > 0) t on t.[Policy Number] = c.MPolicyNumber
where 
    c.deductibleinUSD > 0 
    and c.ProductLine in ('Health','Cas')
order by 
    c.EffDate

提前致谢!

1 个答案:

答案 0 :(得分:2)

如果没有示例数据和所需结果的示例,这是我根据您的问题猜出的最佳数据:

select
    c.MPolicyNumber
  , c.SNumber
  , c.InsuredName
  , c.EffDate
  , c.Renewal
  , c.GPremiumUSD
  , c.Status
  , c.deductibleinUSD
  , t.*
from IT.dbo.Blended c 
  left join (
    select distinct 
        a.[Policy Number]
      , a.[LOB]
      , a.[Primary_R Amount] as Bound_deductibles
      , a.[Primary_R Type]
      , a.[EffDate] as CAS_EffDate
    from IT.dbo.BOUND_TAB a
      inner join IT.dbo.RECORD b
        on a.idxFile = b.[Bound Rater]
    where a.[Primary Retention Amount] > 0
  ) as t 
    on t.[Policy Number] = c.MPolicyNumber
   and c.ProductLine in ('Health','Cas')
   and (c.deductibleinUSD > 0
      or (Primary_R Type = 'Deductible'
        and Bound_deductibles > 0
        )
    )
order by c.EffDate