duangua5742 2018-02-24 20:23
浏览 125

在三个表的内连接中由另一列分组的两列相乘的总和返回错误的值

Sum of a multiplication of two columns grouped by another column in an inner join of three tables returns wrong value. Below are my three tables:

Table1: enter image description here

Table2:

enter image description here

Table3:

enter image description here

My Query is as below:

SELECT c.price, c.quantity, SUM( c.quantity * c.price ) AS price, 
group_concat( a.rate
SEPARATOR '<br>' ) AS rates, c.hsn AS hsn
FROM tax_wa a
INNER JOIN tax_rate_class b ON a.tax_rate_id = b.tax_rate_id
INNER JOIN inv_item c ON b.tax_class_id = c.tax_class_id
WHERE c.invoice_id = '17'
GROUP BY c.hsn

And the result is:

enter image description here

But above one is not correct... To expain it, if you run the below query on the inv_item table (alone, with no joins) you get correct results:

SELECT price, quantity, sum( quantity * price )
FROM `inv_item`
WHERE invoice_id = '17'
GROUP BY hsn

Result is good:

enter image description here

Above result the wrong value calculated if you add all

  • 写回答

1条回答 默认 最新

  • duafagm1066 2018-02-24 20:33
    关注

    Presumably, you want the sum():

    SELECT SUM(c.price),  SUM(c.quantity), SUM( c.quantity * c.price ) AS price, 
           group_concat( a.rate SEPARATOR '<br>' ) AS rates, c.hsn AS hsn
    FROM tax_wa a INNER JOIN
         tax_rate_class b
         ON a.tax_rate_id = b.tax_rate_id INNER JOIN
         inv_item c
         ON b.tax_class_id = c.tax_class_id
    WHERE c.invoice_id = 17
    GROUP BY c.hsn;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题