Мне удалось вывести следующий вывод с этим запросом
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 |
+---------------------+-----------+---------------+------------+--------+------+----------+
Теперь мне нужно вычислить сумму amt и сумму vamt на основе значения vat и refno.
Результат должен быть таким
+---------------------+-----------+---------------+------------+--------+------+----------+
| 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;
Я получаю неправильные итоговые данные.
+---------------------+-----------+---------------+------------+--------+------+---------+
| 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 |
+---------------------+-----------+---------------+------------+--------+------+---------+