加入一个基于Already Joined表的表

时间:2017-02-21 17:09:16

标签: sql join

我正在考虑根据'政策号码加入已加入表格的表格。我正在使用SQL服务器。

查询的第一部分如下:

     select
            MPolicyNumber,
            SNumber,
            IName,
            EDate,
            NewRenewal,
            GPremiumUSD,
            Status,
            deductibleinUSD
    from IT.dbo.Blended
    where deductibleinUSD > 0 and ProductLine in ('Health','Cas')
    order by EDate

我想根据第一个和第二个查询之间匹配的策略编号将上表与下表联系起来

第二次查询:

    select a.[Policy Number], 
    a.[LOB], 
    a.[Primary_R Amount]as Bound_deductibles,
    a.[Primary_R Type],  
    a.[Effective Date] as CAS_EDate
    from IT.dbo.BOUND_TAB a
    inner join IT.dbo.RATER_OF_RECORD b
    on a.idxFile = b.[Bound Rater]
    order by [Policy Number], [idxFile] desc

先谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

这应该这样做(不对语法偏好或效率进行任何更改)

select c.MPolicyNumber, c.SNumber, c.IName, c.EDate, c.NewRenewal, c.GPremiumUSD,
       c.Status, b.deductibleinUSD, t.*
from IT.dbo.Blended c inner join
  (
    select a.[Policy_Number], a.[LOB], a.[Primary_R Amount]as Bound_deductibles,
           a.[Primary_R Type],  a.[Effective Date] as CAS_EDate
    from IT.dbo.BOUND_TAB a inner join 
         IT.dbo.RATER_OF_RECORD b on a.idxFile = b.[Bound Rater]
  ) t ON t.Policy_Number = c.MPolicyNumber
where c.deductibleinUSD > 0 and c.ProductLine in ('Health','Cas')
order by c.EDate