我的挑战如下:我有一个非常简单的SQL查询,它从两个不同的表中获取值,包括SUM值。使用左连接时简单的MYSQL查询总和问题
表1(treatments
)如下:
MedicineDescription | InitAmount | Cost
Mybulen | 90 | 194
表2(treatmentrecords
)如下:
Treatment | AmountGiven
Mybulen | 60
我的SQL查询:
SELECT t.MedicineDescription
, SUM(t.initAMount) startAmount
, SUM(r.AmountGiven) endAmount
, SUM(t.Cost) tCost
FROM treatments t
LEFT
JOIN treatmentrecords r
ON r.Treatment = t.MedicineDescription
GROUP
BY MedicineDescription
和最后。结果:
MedicineDescription | StartAmount | EndAmount | tCost
Mybulen | 90 | 180.00 | 194
正如您可以看到开始金额和tcost计算完美,问题只是为EndAmount返回的SUM不正确。
任何想法将不胜感激!
我无法复制此结果。请参阅https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry
添加一个计数(*)来显示选择找到的记录数量。 –
检查http://sqlfiddle.com/#!9/304ec4/1 – KMS