MySql汇总计算

时间:2015-09-30 12:18:46

标签: mysql aggregate-functions

我创建了这个工作语句,用于使用公式C = A / B * 1000000计算“C”。

这按预期工作,并且C列正确计算:

Select chemicals.Region As Region,
chemicals.Totalt As `A`,
`area`.`m2` As `B`,
((chemicals.Totalt / `area`.`m2`) * 1000000) As C
From (chemicals Join `area` On chemicals.branch = `area`.branch)

现在我需要在汇总报告中使用相同的公式,所以我尝试了这个:

Select chemicals.Region As Region,
sum(chemicals.Totalt) As `A`,
sum(`area`.`m2`) As `B`,
sum( ((chemicals.Totalt / `area`.`m2`) * 1000000)) As C
From (chemicals Join `area` On chemicals.branch = `area`.branch) GROUP BY Region

但是“C”的值未正确计算。

我确信有一种方法可以做到这一点,但只是在“C”计算中添加求和函数是不正确的。 (顺便说一下,A列和B列是正确的。)

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

正如评论中所述,以下解决方案将起作用:

((sum(chemicals.TotalT) / sum(area.m2)) * 1000000) as C