2012-03-21 57 views
0

我已成功地使下面的輸出與此查詢多列組中的MySQL

SELECT DISTINCT 
    IF(purchaseproduct_customerid=0,contactperson,vendorname) AS vendor, 
    IF(purchaseproduct_customerid=0,'',vendortinnumber) AS tin, 
    purchaseproduct_refno AS refno, 
    purchaseproduct_date AS pdate, 
    purchaseproductdetail_amount AS amt, 
    purchaseproductdetail_vat AS vat, 
    purchaseproductdetail_vatamount AS vamt 
    FROM vendor, vendordetails, invoice_purchase_product, invoice_purchase_product_detail 
    WHERE (invoicesale_customerid=customer.customerid AND invoicesale_id=invoicesaledetail_invoicesaleid) 
    OR (invoicesale_contactpersonid=customerdetailsid AND invoicesale_customerid=0 AND invoicesale_id=invoicesaledetail_invoicesaleid) 
    AND purchaseproduct_id=purchaseproductdetail_purchaseproductid; 

+---------------------+-----------+---------------+------------+--------+------+----------+ 
| vendor    | tin  | refno   | pdate  | amt | vat | vamt | 
+---------------------+-----------+---------------+------------+--------+------+----------+ 
| test vendor company | TIN123456 | PO-0001/11-12 | 2012-03-09 | 12000 | 12.5 | 1654.5 | 
| test vendor company | TIN123456 | PO-0001/11-12 | 2012-03-09 | 15000 | 5 | 827.25 | 
| test vendor company | TIN123456 | PO-0001/11-12 | 2012-03-09 | 20000 | 5 |  1103 | 
| test vendor   |   | TEST/632011 | 2012-03-16 | 10000 | 5 | 551.5 | 
| test vendor   |   | TEST/632011 | 2012-03-16 | 12121 | 5 | 668.47 | 
| test vendor   |   | TEST/632011 | 2012-03-16 | 121212 | 5 | 6684.84 | 
| test vendor   |   | TEST/632011 | 2012-03-16 | 1231 | 5 | 67.89 | 
| test vendor   |   | TEST/632011 | 2012-03-16 | 12312 | 5 | 679.01 | 
+---------------------+-----------+---------------+------------+--------+------+----------+ 

現在我必須根據增值稅和refno計算amt和vamt之和的總和。

結果應該是這樣的

+---------------------+-----------+---------------+------------+--------+------+----------+ 
| vendor    | tin  | refno   | pdate  | amt | vat | vamt | 
+---------------------+-----------+---------------+------------+--------+------+----------+ 
| test vendor company | TIN123456 | PO-0001/11-12 | 2012-03-09 | 12000 | 12.5 | 1654.5 | 
| test vendor company | TIN123456 | PO-0001/11-12 | 2012-03-09 | 35000 | 5 | 1930.25 | 
| test vendor   |   | TEST/632011 | 2012-03-16 | 156876 | 5 | 8651.71 | 
+---------------------+-----------+---------------+------------+--------+------+----------+ 

誰能幫我。

SELECT DISTINCT 
IF(purchaseproduct_customerid=0,contactperson,vendorname) AS vendor, 
IF(purchaseproduct_customerid=0,'',vendortinnumber) AS tin, 
purchaseproduct_refno AS refno, 
purchaseproduct_date AS pdate, 
SUM(purchaseproductdetail_amount) AS amt, 
purchaseproductdetail_vat AS vat, 
ROUND(SUM(purchaseproductdetail_vatamount),2) AS vamt 
FROM vendor, vendordetails, invoice_purchase_product, invoice_purchase_product_detail 
WHERE (purchaseproduct_customerid=vendor.vendorid AND purchaseproduct_id=purchaseproductdetail_purchaseproductid) 
OR (purchaseproduct_contactpersonid=vendordetailsid AND purchaseproduct_customerid=0 AND purchaseproduct_id=purchaseproductdetail_purchaseproductid) 
GROUP BY purchaseproduct_refno, purchaseproductdetail_vat 
ORDER BY purchaseproduct_id; 

我得到錯誤的總和和vamt。

+---------------------+-----------+---------------+------------+--------+------+---------+ 
| vendor    | tin  | refno   | pdate  | amt | vat | vamt | 
+---------------------+-----------+---------------+------------+--------+------+---------+ 
| test vendor company | TIN123456 | PO-0001/11-12 | 2012-03-09 | 105000 | 5 | 5790.75 | 
| test vendor company | TIN123456 | PO-0001/11-12 | 2012-03-09 | 36000 | 12.5 | 4963.50 | 
| test vendor   |   | TEST/632011 | 2012-03-16 | 156876 | 5 | 8651.71 | 
+---------------------+-----------+---------------+------------+--------+------+---------+ 

回答

0

認爲這應該這樣做:

SELECT vendor, tin, refno, pdate, sum(amt), vat, sum(vamt) from 
(SELECT DISTINCT 
IF(purchaseproduct_customerid=0,contactperson,vendorname) AS vendor, 
IF(purchaseproduct_customerid=0,'',vendortinnumber) AS tin, 
purchaseproduct_refno AS refno, 
purchaseproduct_date AS pdate, 
purchaseproductdetail_amount AS amt, 
purchaseproductdetail_vat AS vat, 
purchaseproductdetail_vatamount AS vamt 
FROM vendor, vendordetails, invoice_purchase_product, invoice_purchase_product_detail 
WHERE (purchaseproduct_customerid=vendor.vendorid OR purchaseproduct_contactpersonid=vendordetailsid) 
AND purchaseproduct_id=purchaseproductdetail_purchaseproductid) 
GROUP BY refno, vat; 
+0

已經嘗試過這一點。它給出了錯誤的總和()值 – smk3108 2012-03-21 08:15:21

+0

你能粘貼你得到的東西嗎? – barsju 2012-03-21 08:17:33

+0

請再次參考問題,附加結果。 – smk3108 2012-03-21 08:32:27