为什么我在使用LEFT JOIN时获得不同数量的记录

时间:2016-11-04 15:34:15

标签: sql sql-server tsql join

我需要加入多对多的关系。为了做到这一点,我正在使用

LEFT OUTER JOIN  
    (SELECT 
         SICCode, QuoteID 
     FROM  
         NetRate_Quote_Insur_Quote_Busin 
     WHERE 
         QuoteID IN 
              (SELECT DISTINCT QuoteID 
               FROM NetRate_Quote_Insur_Quote_Busin)) nr  ON nr.QuoteID = tblQuotes.QuoteID

目标是引入专栏SICCode。为此,我使用DISTINCT语句来建立多对一的关系。但出于某种原因,我得到了不同数量的记录。

最后LEFT OUTER JOIN是我遇到的问题

完整查询如下所示:

SELECT      
    MONTH(INV.EffectiveDate) AS Effective_Month,
    tblQuotes.PolicyNumber,
    YEAR(INV.EffectiveDate) AS Effective_Year, 
    INV.EffectiveDate,
    INV.[InvoiceDate] as [Billed Date],
    tblQuotes.ExpirationDate as [Policy Expiration Date],
    INV.DueDate,
    dbo.tblFin_InvoiceDetails.AmtBilled AS Written
FROM  
    tblClientOffices
INNER JOIN 
    tblInsureds (NOLOCK) 
INNER JOIN 
    tblFin_Invoices INV
INNER JOIN 
    tblFin_InvoiceDetails ON INV.InvoiceNum = dbo.tblFin_InvoiceDetails.InvoiceNum
INNER JOIN 
    tblCompanyLines (NOLOCK) ON dbo.tblFin_InvoiceDetails.CompanyLineGuid = dbo.tblCompanyLines.CompanyLineGUID 
INNER JOIN 
    lstLines (NOLOCK) ON dbo.tblCompanyLines.LineGUID = dbo.lstLines.LineGUID 
                      AND dbo.tblCompanyLines.LineGUID = dbo.lstLines.LineGUID
INNER JOIN 
    tblProducerLocations (NOLOCK) ON INV.ProducerLocationGUID = tblProducerLocations.ProducerLocationGUID 
    ON tblInsureds.InsuredGUID = INV.InsuredGUID
INNER JOIN 
    tblQuotes (NOLOCK) ON INV.QuoteID = tblQuotes.QuoteID 
                       AND INV.QuoteControlNum = tblQuotes.ControlNo
INNER JOIN 
    lstPolicyTypes (NOLOCK) ON tblQuotes.PolicyTypeID = lstPolicyTypes.PolicyTypeID
INNER JOIN 
    tblSubmissionGroup (NOLOCK) ON tblQuotes.SubmissionGroupGuid = tblSubmissionGroup.SubmissionGroupGUID
INNER JOIN 
    tblCompanyLocations (NOLOCK) ON tblQuotes.CompanyLocationGuid = tblCompanyLocations.CompanyLocationGUID
INNER JOIN 
    tblUsers (NOLOCK) ON INV.UnderwriterUserGUID = tblUsers.UserGUID 
    ON tblClientOffices.OfficeGUID = tblQuotes.IssuingLocationGuid
LEFT OUTER JOIN 
    tblUsers tblUsers_1 (NOLOCK) ON tblSubmissionGroup.InHouseProducerUserGuid = tblUsers_1.UserGUID
LEFT OUTER JOIN  
    (SELECT SICCode, QuoteID 
     FROM NetRate_Quote_Insur_Quote_Busin 
     WHERE QuoteID IN 
           (SELECT DISTINCT QuoteID 
            FROM NetRate_Quote_Insur_Quote_Busin)) nr ON nr.QuoteID = tblQuotes.QuoteID
WHERE 
    (tblInsureds.TestInsured = 0)
    AND (INV.Failed = 0)
    AND (tblFin_InvoiceDetails.ChargeType = 'p')
ORDER BY    
    YEAR(INV.EffectiveDate), 
    MONTH(INV.EffectiveDate), 
    lstLines.LineID  

引入列SICCode并保留相同数量的记录的技巧是什么? 我也尝试过这样做:

LEFT OUTER JOIN NetRate_Quote_Insur_Quote_Busin nr  ON nr.QuoteID = 
                            (
                            SELECT distinct QuoteID from NetRate_Quote_Insur_Quote_Busin
                            where QuoteID =tblQuotes.QuoteID
                            )

但它给了我更多的记录

还试过这个:

    LEFT OUTER JOIN 
                        (   SELECT TOP 1 SICCode, QuoteID 
                            FROM NetRate_Quote_Insur_Quote_Busin 
                            WHERE QuoteID IN 
                                            (SELECT DISTINCT QuoteID 
                                                FROM NetRate_Quote_Insur_Quote_Busin)order by QuoteID desc ) nr ON nr.QuoteID = tblQuotes.QuoteID

但是没有给我带来任何SICCode

1 个答案:

答案 0 :(得分:2)

DISTINCT不会将多对多关系更改为多对一关系。看起来您的数据有多个SICCode值到一个QuoteID,这就是您获得更高记录数的原因。

您可以使用像Rank这样的窗口函数,但这是一个任意选择。

您可以尝试转动/取消忽略SICCode列,但这会为每个可能的值提供1列,如果列中有大量唯一值,则无法使用。

最后一个选项是创建一个新表格,其中包含QuoteID列和SICCodes列,然后填充SICCodes作为逗号分隔的代码列表。

总之,除非您愿意丢失数据或只有少数几个值,否则没有简单的方法可以将多对多关系更改为多对一关系。