SQL 2使用Sum和Group By左外连接

时间:2016-02-04 17:25:18

标签: sql sql-server join

寻找一些指导。我正在尝试在我的投诉管理系统中运行报告。按年份,位置,子类别的投诉,显示TotalCredits(子表)和TotalsCwts(子表)的总计以及总计ExternalRootCause(在主表上)。

这是我的SQL,但未正确计算TotalCwts和TotalCredits。它为每个子记录计算1次,而不是每个主记录的总计。

SELECT 
  dbo.Complaints.Location, 
  YEAR(dbo.Complaints.ComDate) AS Year, 
  dbo.Complaints.ComplaintSubcategory,
  COUNT(Distinct(dbo.Complaints.ComId)) AS CustomerComplaints, 
  SUM(DISTINCT CASE WHEN (dbo.Complaints.RootCauseSource = 'External' ) THEN 1 ELSE 0 END) as ExternalRootCause,
  SUM(dbo.ComplaintProducts.Cwts) AS TotalCwts, 
  Coalesce(SUM(dbo.CreditDeductions.CreditAmount),0) AS TotalCredits

FROM dbo.Complaints 
  JOIN dbo.CustomerComplaints
    ON dbo.Complaints.ComId = dbo.CustomerComplaints.ComId 
  LEFT OUTER JOIN dbo.CreditDeductions
    ON dbo.Complaints.ComId = dbo.CreditDeductions.ComId
  LEFT OUTER JOIN dbo.ComplaintProducts
    ON dbo.Complaints.ComId = dbo.ComplaintProducts.ComId

WHERE
  dbo.Complaints.Location =  Coalesce(@Location,Location)  

GROUP BY 
  YEAR(dbo.Complaints.ComDate),
  dbo.Complaints.Location, 
  dbo.Complaints.ComplaintSubcategory
ORDER BY 
  [YEAR] desc,
  dbo.Complaints.Location, 
  dbo.Complaints.ComplaintSubcategory

数据结果

Location | Year | Subcategory   | Complaints | External RC | Total Cwts | Total Credits
---------------------------------------------------------------------------------------
 Boston  | 2016 | Documentation |       1    |       0     |      8     |   8.00 

应该读取数据

Location | Year | Subcategory   | Complaints | External RC | Total Cwts | Total Credits
---------------------------------------------------------------------------------------
 Boston  | 2016 | Documentation |         1   |      0      |     4     |   2.00 

以上数据反映1起投诉有4个产品记录,每个1cwt,2个信用记录,每个1.00。

我需要在查询中更改哪些内容,还是应该以不同的方式处理此查询?

1 个答案:

答案 0 :(得分:0)

问题是1起投诉有2个扣减和4个产品。当您以这种方式加入时,它将返回投诉的每个Deduction / Product组合,当您查看时会提供8行。

一个应该在这里工作的解决方案是不直接查询Dedustion和Product表;查询每个投诉每个表返回一行的查询。换句话说,替换:

            LEFT OUTER JOIN dbo.CreditDeductions ON dbo.Complaints.ComId = dbo.CreditDeductions.ComId
            LEFT OUTER JOIN dbo.ComplaintProducts ON dbo.Complaints.ComId = dbo.ComplaintProducts.ComId

...有了这个 - 仅显示扣减表,您可以计算出产品:

LEFT OUTER JOIN (
    select ComId, count(*) CountDeductions, sum(CreditAmount) CreditAmount
    from dbo.CreditDeductions
    group by ComId
) d on d.ComId = Complaints.ComId

您必须将对dbo.CreditDedustions的引用更改为d(或您想要调用的任何内容)。

一旦你完成了这两项工作,那么每次投诉你就会得到一个,这将导致每个投诉中有一行包含来自两个子表的计数和总数。