如何用多个sum修复查询mysql

时间:2016-09-06 04:02:35

标签: mysql sum

我有来自sum函数的查询数据:

ROUND(((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2)/100,2) AS nominal_persentasi,
ROUND((((nominal)*12) * ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))*(1.1/100)/100,2) AS tambah_persentasi,
ROUND((((nominal)*12) *  ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))+((((nominal)*12) *  ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))*(1.1/100))/100,2) AS total_penyesuaian

结果是:

nominal_persentasi | tambah_persentasi | total_penyesuaian
12.000               3.000               1.203.000

产生的结果应该是15,000,为什么会发生?

我尝试对变量nominal_persentasi + tambah_persentasi求和,但结果为0

1 个答案:

答案 0 :(得分:1)

您在总数中错过了100的分组。因此,您不必添加12,0003,000来获取15,000,而是实际添加12,000,0003,000以获取12,003,000

SELECT ROUND(( (nominal)*12) *  ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2)/100,2) AS nominal_persentasi,
       ROUND((((nominal)*12) *  ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))*(1.1/100)/100,2) AS tambah_persentasi,
       ROUND((((nominal)*12) *  ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2)/100) + ((((nominal)*12) *  ROUND((SUM((a.NCI)/3*(60/100))+SUM((b.NSI)/3*(40/100)))/3,2))*(1.1/100))/100, 2) AS total_penyesuaian
FROM yourTable                      -- your query was missing this division by 100           ^^^