为什么我的JOIN中有重复记录

时间:2016-08-25 14:54:41

标签: sql-server tsql join reporting-services

我正在从表<Products> <ProductExport> <R001 Retail=\"0\" Rank=\"1\" Code=\"001\" /> </ProductExport> <ProductExport> <R002 Retail=\"0\" Rank=\"2\" Code=\"002\" /> </ProductExport> <ProductExport> <R003 Retail=\"0\" Rank=\"3\" Code=\"003\" /> </ProductExport> <ProductExport> <R004 Retail=\"0\" Rank=\"4\" Code=\"004\" /> </ProductExport> <ProductExport> <R011 Retail=\"0\" Rank=\"7\" Code=\"011\" /> </ProductExport> </Products> 检索数据,其中我有列ProductionReportMetrics。然后,对于该结果集,我需要获得NetRate_QuoteID列。

为了获得Description列,我需要加入3个表:

  • NetRate_Quote_Insur_Quote
  • NetRate_Quote_Insur_Quote_Locat
  • NetRate_Quote_Insur_Quote_Locat_Liabi

但在那之后我的保费完全没了。

我在这里做错了什么?

Description

enter image description here

enter image description here

我认为此表中的问题: enter image description here

此查询中缺少什么?

SELECT QLL.Description,
       QLL.ClassCode,
       prm.NetRate_QuoteID,
       QL.LocationID,
       ISNULL(SUM(premium),0) AS NetWrittenPremium,
       MONTH(prm.EffectiveDate) AS EffMonth
FROM ProductionReportMetrics prm    
LEFT JOIN NetRate_Quote_Insur_Quote Q
    ON prm.NetRate_QuoteID = Q.QuoteID
INNER JOIN NetRate_Quote_Insur_Quote_Locat QL
    ON Q.QuoteID = QL.QuoteID
INNER JOIN NetRate_Quote_Insur_Quote_Locat_Liabi QLL
    ON QL.LocationID = QLL.LocationID       
WHERE YEAR(prm.EffectiveDate) = 2016 AND
      CompanyLine = 'Ironshore Insurance Company' 
GROUP BY MONTH(prm.EffectiveDate),
         QLL.Description,
         QLL.ClassCode,
         prm.NetRate_QuoteID,
         QL.LocationID 

现在说

select 
        ClassCode,
        QLL.Description,
        sum(Premium)

from    ProductionReportMetrics prm
LEFT JOIN  NetRate_Quote_Insur_Quote Q ON prm.NetRate_QuoteID = Q.QuoteID
LEFT JOIN NetRate_Quote_Insur_Quote_Locat QL ON Q.QuoteID = QL.QuoteID  
LEFT JOIN
            (SELECT * FROM NetRate_Quote_Insur_Quote_Locat_Liabi nqI 
                JOIN ( SELECT LocationID, MAX(ClassCode) 
                FROM NetRate_Quote_Insur_Quote_Locat_Liabi GROUP BY LocationID ) nqA 
                    ON nqA.LocationID = nqI.LocationID ) QLL ON QLL.LocationID = QL.LocationID  
where   Year(prm.EffectiveDate) = 2016 AND CompanyLine = 'Ironshore Insurance Company' 
GROUP BY Q.QuoteID,QL.QuoteID,QL.LocationID

enter image description here

1 个答案:

答案 0 :(得分:3)

看起来DVT基本上找到了答案。由于连接而获得不同数量(即重复行)的唯一原因是其中一个连接表与主表的关系不是1:1。
我建议你快速检查这些表格,查找表格数量。

    --this should be your baseline count
    SELECT COUNT(*)
    FROM ProductionReportMetrics
    GROUP BY MONTH(prm.EffectiveDate),
        prm.NetRate_QuoteID

    --this will be a check against the first joined table.  
    SELECT COUNT(*)
    FROM NetRate_Quote_Insur_Quote Q
    WHERE QuoteID IN
        (SELECT NetRate_QuoteID 
            FROM ProductionReportMetrics
            GROUP BY MONTH(prm.EffectiveDate),
                prm.NetRate_QuoteID)

基本上,您需要对每个连接的表执行类似的检查。如果任何连接表是分组语句的一部分,请确保它们也在计数检查语句的分组中。还要确保更改check count语句的WHERE子句以使用您正在使用的join子句列 一旦找到返回错误行数的表,您就会得到关于导致问题的表的答案。然后你将只需要决定如何将该表限制为不同的行(某种类型的聚合) 这个建议实际上只是为了向您展示如何QA这个特定的查询。将其分解为尽可能小的部分。在这种情况下,我们知道这是一个导致问题的连接,所以一次取一个连接直到找到违规者。