mysql中的多列组

时间:2012-03-21 07:52:05

标签: mysql grouping

我已设法通过此查询带来以下输出

    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;

我得到了错误的总和amt和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 |
+---------------------+-----------+---------------+------------+--------+------+---------+

1 个答案:

答案 0 :(得分: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;