这是我第一次寻求您的帮助,
实际上,我必须创建一个查询,并为此做了一个类似的示例。我有两张桌子
Report (ReportID, Date, headCount) Production(ProdID, ReportID, Quantity)
我的问题是使用此查询,我得到了错误的结果,
SELECT Report.date, SUM(Report.HeadCount) AS SumHeadCount, SUM(Production.Quantity) AS SumQuantity FROM Report INNER JOIN Production ON Report.ReportID = Production.ReportID GROUP BY Date ORDER BY Date
我猜有些行已被计算不止一次,您能帮我一下吗?
编辑
如果我运行查询以按天分组总人数,则会得到:
date Headcount 7/2/2012 1843 7/3/2012 1802 7/4/2012 1858 7/5/2012 1904
同样对于生产数量,我得到:
2012-07-02 8362 2012-07-03 8042 2012-07-04 8272 2012-07-05 9227
但是当我将两个查询合并时,我得到一个错误的查询,我期望在7月2日8362的数量为1843,但是我得到:
day TotalHeadcount totalQty 7/2/2012 6021 8362 7/3/2012 7193 8042 7/4/2012 6988 8272 7/5/2012 7197 9227
使用以下每个日期的组记录
SELECT ReportSummary.ReportDate, SUM(ReportSummary.SumHeadCount) AS SumHeadCount, SUM(ProductionSummary.SumQuantity) AS SumQuantity FROM ( SELECT Report.ReportDate, SUM(Report.HeadCount) AS SumHeadCount FROM Report GROUP BY Report.ReportDate ) AS ReportSummary INNER JOIN ( SELECT Report.ReportDate, Sum(Production.Quantity) AS SumQuantity FROM Production INNER JOIN Report ON Report.ReportID = Production.ReportID GROUP BY Report.ReportDate ) AS ProductionSummary ON ReportSummary.ReportDate = ProductionSummary.ReportDate GROUP BY ReportSummary.ReportDate ORDER BY ReportSummary.ReportDate