2017-02-21 110 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