如何使用基于列值的条件来连接表

时间:2017-01-23 23:50:15

标签: sql-server tsql join case

我的数据如下:

enter image description here

正如您所看到的,我有41美元的重复Premium。 如果我在我的JOIN条件中说:

` LEFT OUTER JOIN Catalytic_vw_AggregateScheduleUniqueGUID agg ON agg.QuoteGuid = dbo.tblQuotes.QuoteGUID AND agg.guid = CL.CompanylocationGUID
            AND   agg.policyNumber IS NOT NULL 

然后我在NULL列中看不到PolicyNumberFromRater值。  但是我不希望仅针对特定LineGUID看到NULL,而不是所有这些。

基本上我想说:仅对AND agg.policyNumber IS NOT NULL使用条件LineGUID = CF144437... 但如果它将是其他LineGUID,那么我想在`PolicyNumberFromRater中看到NULL

当我一起加入桌子时,我怎么能这样做?

包含LineGUID的表格tbl.Quotes也可以在JOIN中找到。

这是我的完整JOIN

 select PolicyNumber
    FROM       tblFin_Invoices INV 
                    INNER JOIN dbo.tblQuotes ON INV.QuoteID = tblQuotes.QuoteID
                    INNER JOIN tblFin_InvoiceDetails INVD ON INV.InvoiceNum = INVD.InvoiceNum 
                    INNER JOIN dbo.tblQuoteDetails ON dbo.tblQuotes.QuoteGUID = dbo.tblQuoteDetails.QuoteGuid AND tblQuoteDetails.CompanyLineGuid = CF144437-F128-4B77-AC19-877247347D02
INVD.CompanyLineGuid    
                    LEFT OUTER  JOIN dbo.tblCompanyLines ON dbo.tblQuoteDetails.CompanyLineGuid = dbo.tblCompanyLines.CompanyLineGUID 
                    LEFT OUTER JOIN dbo.tblCompanyLocations AS CL ON dbo.tblCompanyLines.CompanyLocationGUID = CL.CompanyLocationGUID 
                LEFT OUTER  JOIN tblCompanyLocations ON tblQuotes.CompanyLocationGuid = tblCompanyLocations.CompanyLocationGUID
    ---------/*This is where I need the condition*/
                LEFT OUTER JOIN Catalytic_vw_AggregateScheduleUniqueGUID agg ON agg.QuoteGuid = dbo.tblQuotes.QuoteGUID AND agg.guid = CL.CompanylocationGUID
                AND agg.policyNumber IS NOT NULL 
   --(use "AND agg.policyNumber IS NOT NULL" only for LineGUID = CF144437...)      
                    LEFT OUTER JOIN         tblSubmissionGroup ON tblQuotes.SubmissionGroupGuid = tblSubmissionGroup.SubmissionGroupGUID
                    LEFT OUTER JOIN         tblUsers ON INV.UnderwriterUserGUID = tblUsers.UserGUID
                    LEFT OUTER JOIN         tblClientOffices ON tblQuotes.QuotingLocationGuid = tblClientOffices.OfficeGUID
        LEFT OUTER JOIN  tblInsureds ON tblSubmissionGroup.InsuredGuid = tblInsureds.InsuredGuid
        LEFT OUTER JOIN  lstPolicyTypes ON tblQuotes.PolicyTypeID = lstPolicyTypes.PolicyTypeID

2 个答案:

答案 0 :(得分:2)

如果你只想处理你所描述的情况,即处理所有列都重复的记录,除了PolicyNumberFromRater,其中一个或多个记录可能有NULL,那么我认为你可以使用NULL汇总GROUP BY个值:

SELECT MAX(PolicyNumberFromRater) AS PolicyNumberFromRater,
       LineGUID,
       Premium,
       Carrier,
       -- other columns
FROM tblFin_Invoices INV 
INNER JOIN dbo.tblQuotes
    ON INV.QuoteID = tblQuotes.QuoteID
-- other joins
GROUP BY LineGUID,
         Premium,
         Carrier,
         -- all other columns in SELECT except for PolicyNumberFromRater

答案 1 :(得分:1)

你能尝试这样吗,当LineGUID是其他值并且agg.policyNumber为空时,条件将是'!=' *'(真)

LEFT OUTER JOIN Catalytic_vw_AggregateScheduleUniqueGUID agg ON agg.QuoteGuid = dbo.tblQuotes.QuoteGUID AND agg.guid = CL.CompanylocationGUID
            AND  ISNULL(agg.policyNumber,'')!=CASE WHEN LineGUID = 'CF144437...' THEN '' ELSE '*' END