简体   繁体   中英

sql not calculating sum correctly

I've got the following sql query, it seems to be returning data, but the sum of the invoice_total (4th column) is not calculating properly. At the moment although I have about 50 records in purchase files with the duplicate supplierID, I currently only have 4 individual supplier records filled with the invoice_total. The returned result is not what I have put in.

SELECT p.orderID, s.supplier, SUM(IFNULL(pl.line_price,0)) AS total_order, SUM(IFNULL(p.invoice_total,0)) AS invoice_total  
  FROM purchase p 
  LEFT JOIN purchase_line pl 
  ON p.orderID = pl.orderID 
  LEFT JOIN supplier s 
  ON p.supplierID = s.supplierID 
  WHERE (p.date BETWEEN '2015-01-01' AND '2015-12-30')
  GROUP BY p.supplierID 
  ORDER BY p.supplierID ASC

Table definitions are as follows along with sample data purchase

  orderID, date, supplierID, supplier_ref, invoice_total
  001, 2015-11-15, 1, abc, 11300000 
  002, 2015-11-15, 2, def, 23900000
  003, 2015-11-15, 3, ghi, 68265000
  004, 2015-11-15, 4, jkl, 31300000
  005, 2015-11-15, 5, lmn, [Null]

  purchase_line
  orderID, item_price, qty, line_price
  001,100000,50000, (that would be line_price*qty)
  001,200000,25000,
  001,300000,10000,
  002,400000,20000,
  003,300000,30000,
  004,200000,20000,

  supplier
  supplierID, supplier, address1, address2, zip
  1, Apple 
  2, Microsoft,
  3, Oracle
  4, SAP
  5, IBM

The data that seems to be returned for the total_invoice field is. Bear in mind I have only populated one row of data with the invoice_total filled in. However I have lots of records with Null in that field which are associated to supplier 1 or supplier 2.

  supplierID, invoice_total
  1, 700600000
  2, 95600000
  3, 136530000

The sum of the invoice_totals are incorrect as invoice_total values can be repeated, as each order can have multiple rows in purchase_line . In order to resolve this, you will need to aggregate twice, first on supplierid and order and finally on supplierid only.

 SELECT supplierid ,
       SUM(total_order) AS total_order ,
       SUM(invoice_total) AS invoice_total
 FROM   ( SELECT    p.supplierID ,
                p.orderID ,
                SUM(IFNULL(pl.line_price, 0)) AS total_order ,
                IFNULL(p.invoice_total, 0) AS invoice_total
        FROM        purchase p
                LEFT JOIN purchase_line pl ON p.orderID = pl.orderID
                LEFT JOIN supplier s ON p.supplierID = s.supplierID
        WHERE   ( p.date BETWEEN '2015-01-01' AND '2015-12-30' )
        GROUP BY    p.supplierID ,
                p.orderID
       ) details
 GROUP BY   supplierID
 ORDER BY   supplierID ASC

Just to clarify the inner query, for Apple this IFNULL(p.invoice_total, 0) picks one of the three (duplicate) invoice totals. You can achieve the same using MIN(IFNULL(p.invoice_total, 0)) or MAX(IFNULL(p.invoice_total, 0)) as the invoice_totals are all duplicates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM