我正在尝试运行销售报告查询。
我使用SUM()
收到的购买总额不正确:似乎问题可能出在我的GROUP BY
上。
查询:
SELECT salesreport.canprevid,
storelist.organization,
SUM(quantity * unitprice * (100 - discount) / 100) AS total,
storelist.external_manager,
storelist.manager
FROM storelist
INNER JOIN salesreport
ON storelist.store_id = salesreport.canprevid
WHERE salesreport.date >= "2016-01-01"
AND salesreport.date <= "2016-01-31"
GROUP BY canprevid
ORDER BY organization DESC;
正如所料,我能够拉动:
|canprevid|organization|total (for the month of january 2016)| external manager| manager
然而,当我使用total
进行总计SUM()
时,total
不等于什么2016年1月的真实总金额。
我不确定为什么会删除某些记录或GROUP BY
导致计算出现问题。
答案 0 :(得分:1)
将storelist.organization
添加到GROUP BY
:
SELECT salesreport.canprevid,
storelist.organization,
SUM(quantity * unitprice * (100 - discount) / 100) AS total,
storelist.external_manager,
storelist.manager
FROM storelist
INNER JOIN salesreport
ON storelist.store_id = salesreport.canprevid
WHERE salesreport.date >= "2016-01-01"
AND salesreport.date <= "2016-01-31"
GROUP BY salesreport.canprevid, storelist.organization
ORDER BY storelist.organization DESC;