2017-02-21 117 views
2

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

以下查询:三个表共混,BOUND_TAB和RECORD

第二件事情是,我还需要筛选结果使得我在“混纺”表或Primary_R类型使用策略与deductibleinUSD> 0 ='来自内部选择的“Deductible”和Primary_R Amount> 0。如果“Blended”或“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 

在此先感谢!

+2

问题是什么? –

+0

对于你的第一个'问题':你为什么需要'左外连接'?什么阻止你只是在你的查询中替换'INNER'?对于第二个问题:只需在你的'WHERE'子句中指定那些过滤器即可。现在,'ProductLine'包含其他过滤器还是仅包含'deductibleUSD'? –

回答

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