左连接返回错误的总数

时间:2016-01-20 10:56:48

标签: sql join count aggregate

我有两张桌子; CalendarCasesCases包含所有交易数据。这意味着,如果交易没有在一个月内发生,例如6月份,则6月份将不会有任何行。但是,如果6月份没有交易,我希望在那里说明{6}对{6}。

这里有0,其中包含我想要在结果集中的所有月份。但是,无论我申请左/右/外连接,我都无法获得这些行告诉我表中有Calendar个事务。

以下是我可以提出的最新查询,也请找到结果集。虽然结果集显示正确的月数,但最终总数不正确,并且每个月都相同。我哪里错了?

请帮忙!

QUERY

0

结果设置

    SELECT cal.Org
      ,cal.CaseProduct
      ,cal.FaultGroup
      ,cal.ManufacturingMonth as ManufacturingMonth
      ,cal.ClosedMonth as ClosedMonth --ClosedMonthStart as ClosedMonth
      ,ISNULL(count(c.CaseProduct), 0) as TotalCases
FROM Calendar cal
LEFT JOIN [QualityForecasting].[dbo].[Cases] c
   ON cal.ManufacturingMonth = c.[ManufacturingDate]
  AND cal.CaseProduct = c.CaseProduct
  AND cal.FaultGroup = c.FaultGroup 
  AND c.CaseNature IN ('Missing / Damage Case', 'Compressor 2 Years Warranty', 'Compressor 3 Years Warranty', 'Showroom', 'Warehouse', 'Warranty (2-12 Months)', 'Within One Month Customer', 'From 31 to 90 Days Customer', 'From 91 to 120 Days Customer', 'Within 5~6 Months')
  AND c.CaseType IN ('Depot', 'Field', 'Direct', 'Field to Depot', 'Field to Mobile')
  AND c.LastTaskStatus NOT IN ('Transferred to Workshop', 'Mobile Required')
  AND SUBSTRING(c.CaseModelName, 1, 3) = 'DPL'
WHERE cal.ManufacturingMonth = '2012-08-01'
  AND cal.ClosedMonth <= '2014-01-01'  
  AND cal.Org = 'DPL'
  AND cal.FaultGroup = 'COMPRESSOR'
GROUP BY cal.Org
        ,cal.CaseProduct
        ,cal.FaultGroup
        ,cal.ManufacturingMonth 
        ,cal.ClosedMonth;

更新1

案例表结构:

Org CaseProduct FaultGroup  ManufacturingMonth  ClosedMonth TotalCases
DPL REF COMPRESSOR  2012-08-01  2012-08-01  65
DPL REF COMPRESSOR  2012-08-01  2012-09-01  65
DPL REF COMPRESSOR  2012-08-01  2012-10-01  65
DPL REF COMPRESSOR  2012-08-01  2012-11-01  65
DPL REF COMPRESSOR  2012-08-01  2012-12-01  65
DPL REF COMPRESSOR  2012-08-01  2013-01-01  65
DPL REF COMPRESSOR  2012-08-01  2013-02-01  65
DPL REF COMPRESSOR  2012-08-01  2013-03-01  65
DPL REF COMPRESSOR  2012-08-01  2013-04-01  65
DPL REF COMPRESSOR  2012-08-01  2013-05-01  65
DPL REF COMPRESSOR  2012-08-01  2013-06-01  65
DPL REF COMPRESSOR  2012-08-01  2013-07-01  65
DPL REF COMPRESSOR  2012-08-01  2013-08-01  65
DPL REF COMPRESSOR  2012-08-01  2013-09-01  65
DPL REF COMPRESSOR  2012-08-01  2013-10-01  65
DPL REF COMPRESSOR  2012-08-01  2013-11-01  65
DPL REF COMPRESSOR  2012-08-01  2013-12-01  65
DPL REF COMPRESSOR  2012-08-01  2014-01-01  65

日历表格结构

CaseNumber  CaseProduct ManufacturingDate   FaultGroup
15-04-555333    REF 2012-10-01  GAS CHARGING                                      
14-11-513482    REF 2012-10-01  EVAPORATOR                                        
15-04-556248    REF 2012-10-01  GASKET                                            
15-04-564243    REF 2012-10-01  COMPRESSOR                                        
15-06-599612    REF 2012-10-01  WIRING - PRODUCT                                  

1 个答案:

答案 0 :(得分:0)

LEFT JOIN就属于这种情况。最好只在ON子句中保留连接条件,因此这应该有效:

SELECT cal.Org
      ,cal.CaseProduct
      ,cal.FaultGroup
      ,cal.ManufacturingMonth as ManufacturingMonth
      ,cal.ClosedMonth as ClosedMonth --ClosedMonthStart as ClosedMonth
      ,ISNULL(count(c.CaseProduct), 0) as TotalCases
FROM Calendar cal
LEFT JOIN [QualityForecasting].[dbo].[Cases] c
   ON cal.ManufacturingMonth = c.[ManufacturingDate]
  AND cal.CaseProduct = c.CaseProduct
  AND cal.FaultGroup = c.FaultGroup 
  AND c.CaseNature IN ('Missing / Damage Case', 'Compressor 2 Years Warranty', 'Compressor 3 Years Warranty', 'Showroom', 'Warehouse', 'Warranty (2-12 Months)', 'Within One Month Customer', 'From 31 to 90 Days Customer', 'From 91 to 120 Days Customer', 'Within 5~6 Months')
  AND c.CaseType IN ('Depot', 'Field', 'Direct', 'Field to Depot', 'Field to Mobile')
  AND c.LastTaskStatus NOT IN ('Transferred to Workshop', 'Mobile Required')
  AND SUBSTRING(c.CaseModelName, 1, 3) = 'DPL'
WHERE cal.ManufacturingMonth = '2012-08-01'
  AND cal.ClosedMonth <= '2014-01-01'  
  AND cal.Org = 'DPL'
  AND cal.FaultGroup = 'COMPRESSOR'
GROUP BY cal.Org
        ,cal.CaseProduct
        ,cal.FaultGroup
        ,cal.ManufacturingMonth 
        ,cal.ClosedMonth;

UPD 1 这应该有效。以下是尝试其他任何事情的绝望尝试:

SELECT cal.Org
      ,cal.CaseProduct
      ,cal.FaultGroup
      ,cal.ManufacturingMonth
      ,cal.ClosedMonth
      ,(SELECT count(*)
        FROM [QualityForecasting].[dbo].[Cases] c
        WHERE cal.ManufacturingMonth = c.[ManufacturingDate]
          AND cal.CaseProduct = c.CaseProduct
          AND cal.FaultGroup = c.FaultGroup 
          AND c.CaseNature IN ('Missing / Damage Case', 'Compressor 2 Years Warranty', 'Compressor 3 Years Warranty', 'Showroom', 'Warehouse', 'Warranty (2-12 Months)', 'Within One Month Customer', 'From 31 to 90 Days Customer', 'From 91 to 120 Days Customer', 'Within 5~6 Months')
          AND c.CaseType IN ('Depot', 'Field', 'Direct', 'Field to Depot', 'Field to Mobile')
          AND c.LastTaskStatus NOT IN ('Transferred to Workshop', 'Mobile Required')
          AND SUBSTRING(c.CaseModelName, 1, 3) = 'DPL'
       ) as TotalCases
FROM (SELECT DISTINCT
         Org
        ,CaseProduct
        ,FaultGroup
        ,ManufacturingMonth
        ,ClosedMonth
      FROM Calendar
      WHERE ManufacturingMonth = '2012-08-01'
        AND ClosedMonth <= '2014-01-01'  
        AND Org = 'DPL'
        AND FaultGroup = 'COMPRESSOR'
     ) cal

UPD 2 因为&#34;绝望的尝试&#34;产生了相同的结果,是时候用各个参数组合运行子查询,看看数据是怎么回事:

SELECT *
FROM [QualityForecasting].[dbo].[Cases] c
WHERE cal.ManufacturingMonth = /* particular c.ManufacturingDate */
  AND cal.CaseProduct =        /* particular c.CaseProduct       */
  AND cal.FaultGroup =         /* particular c.FaultGroup        */
  AND c.CaseNature IN ('Missing / Damage Case', 'Compressor 2 Years Warranty', 'Compressor 3 Years Warranty', 'Showroom', 'Warehouse', 'Warranty (2-12 Months)', 'Within One Month Customer', 'From 31 to 90 Days Customer', 'From 91 to 120 Days Customer', 'Within 5~6 Months')
  AND c.CaseType IN ('Depot', 'Field', 'Direct', 'Field to Depot', 'Field to Mobile')
  AND c.LastTaskStatus NOT IN ('Transferred to Workshop', 'Mobile Required')
  AND SUBSTRING(c.CaseModelName, 1, 3) = 'DPL'