2011-11-17 73 views
0

我有兩個表。使用SUM進行計算,查詢不起作用?

一) ai_account

enter image description here

B) ai_order_product

enter image description here

我想要做特定supplier_id一些計算。

1,總金額,我想這樣做 SUM(ai_order_product.quantity * ai_order_product.cost)

2,amountPaid,這是由供應商支付的總金額,這將是 像SUM (ai_account.amount)參考supplier_id。

3)平衡,這將通過SUM來計算(ai_order_product.quantity * ai_order_product.cost) - SUM(ai_invoice.amount)

4)lastPayment日期,這將是MAX(ai_account.addDate)。

我試着做這樣的事情。

SELECT SUM(op.quantity * op.cost) as totalAmount, 
     SUM(ac.amount) as amountPaid, 
     SUM(op.quantity * op.cost) - SUM(ac.amount) as balance, 
     MAX(ac.addDate) as lastPayment 
    FROM ai_order_product op 
     LEFT JOIN ai_account ac 
      ON (op.supplier_id = ac.trader_id) 
    WHERE op.supplier_id = 42 

它不能正常工作,它獲取一些意想不到的值,而對於被預計上述結果是,

for supplier_id = 42, 
1) totalAmount = 1375, 
2) amountPaid = 7000, 
3) balance = -5625, 
4) lastPayment = 2011-11-23 

and for supplier_id = 35, 
1) totalAmount = 1500, 
2) amountPaid = 43221, 
3) balance = -41721, 
4) lastPayment = 2011-11-28 

and for supplier_id = 41 
1) totalAmount = 0 
2) amountPaid = 3000, 
3) balance = -3000, 
4) lastPayment = 2011-11-09 

我想supplier_id獲取一行。

P.S:我剛剛輸入了一些虛擬值,這就是爲什麼計算大多是負值,而在應用中計算值是正值。

+0

您的表格中沒有任何'trader_id' ... –

+0

這是錯誤的,我將其更改爲supplier_id :) –

+0

會發生什麼? – drdwilcox

回答

2

那是因爲每個「ai_order_product」行都會被多次計數(ai_account表中每行存在一次)。

試試這個:

SELECT 
    op.totalAmount as totalAmount 
    , SUM(ac.amount) as amountPaid 
    , op.totalAmount - SUM(ac.amount) as balance 
    , MAX(ac.addDate) as lastPayment 
FROM (
    select supplier_id, sum(quantity * cost) as totalAmount 
    from ai_order_product 
    group by supplier_id) op 
LEFT JOIN ai_account ac ON (op.supplier_id = ac.trader_id) 
WHERE op.supplier_id = 42 

這可能是稍微偏離,但這個一般的邏輯應該工作。

+0

,這將引發以下error.'ERROR 1054(42S22):在「字段未知列「op.quantity」 list'' –

+0

啊,看到編輯,只需要刪除‘OP’。在總和 –

+0

這工作正常,但我有一個情況在這裏,我認爲生病發布它作爲新的問題。謝謝@Darek。 :) –

1

你有使用聚合函數時,像SUM SELECT語句使用國家GROUP BY

SELECT op.supplier_id as supplierId, 
     SUM(op.quantity * op.cost) as totalAmount, 
     SUM(ac.amount) as amountPaid, 
     SUM(op.quantity * op.cost) - SUM(ac.amount) as balance, 
     MAX(ac.addDate) as lastPayment 
    FROM ai_order_product op 
     LEFT JOIN ai_account ac 
      ON (op.supplier_id = ac.trader_id) 
    GROUP BY op.supplier_id 
    HAVING supplierId = 42 
+0

這不起作用,它提取將相乘的結果,例如用於ID 42它提取用於總金額= 2750 wheras它應該是1375和支付金額它取出14000 wheras我期待7000 –

+0

@Andreas:通過僅要求如果組你在查詢中有非聚合的字段。 –

+0

@MarcB,你有任何解決方案建議? –

1
SELECT 
    SUM(op.quantity * op.cost) as totalAmount 
    , ac2.amountPaid 
    , SUM(op.quantity * op.cost) - ac2.balance 
    , ac2.lastPayment 
FROM ai_order_product op 
LEFT JOIN (SELECT 
      ac.supplier_id 
      , MAX(ac.addDate) as lastPayment 
      , SUM(ac.amount) as balance 
      FROM ai_account ac 
      WHERE (op.supplier_id = ac.supplier_id) 
      GROUP BY ac.supplier_id) ac2 ON (ac2.supplier_id = op.supplier_id) 
WHERE op.supplier_id = 42 
GROUP BY op.supplier_id 

當您選擇多個supplier_id時,group by子句將啓動。

+0

我很弱,在MySQL中,無法弄清楚,這個查詢根本不起作用。它會發出以下錯誤。 '你的SQL語法有錯誤;檢查對應於你的MySQL服務器版本正確的語法使用近「AC2 ON(ac2.trader_id = op.supplier_id) WHERE op.supplier_id = 42 GROUP BY運算」在行13' –

+0

固定查詢交流手冊.trader_id - > supplier_id – Johan

+0

實際現場被ac.trader_id,我改變了它supplier_id爲了避免混淆,甚至進行更改後不糾正它給了我這個錯誤的字段後爲我工作:( –

0

請注意,您的預期值和實際值會加倍。輸入您的樣本數據和運行查詢後,我得到(對於supplier_id = 42)

+-------------+------------+---------+-------------+ 
| totalAmount | amountPaid | balance | lastPayment | 
+-------------+------------+---------+-------------+ 
|  2750 |  14000 | -11250 | 2011-11-23 | 
+-------------+------------+---------+-------------+ 

那是因爲你已經得到了在符合加入條件的每個表2行,造成的結果增加了一倍。

+0

是啊,這是發生了什麼,我沒有料到這個結果。我應該怎麼做? –

0

我認爲你需要首先得到子總數爲一個表,讓你只有1行回來。因此這裏的內部選擇返回最大值和總和,所以當你加入它時你只能得到1行。

SELECT SUM(op.quantity * op.cost) as totalAmount, 
     ac.addDate as lastPayment, 
     ac.amount as amountPaid, 
     SUM(op.quantity * op.cost) - SUM(ac.amount) as balance 
    FROM ai_order_product op 
     INNER JOIN (
      SELECT max(IaiaddDate) as addDate, sum(iai.amount) as Amount, iai.supplier_ID 
      FROM ai_account iai 
      Group by supplier_ID) ac 
     ON AC.Supplier_ID = Op.Supplier_ID 
    WHERE op.supplier_id = 42 

集團通過Ac.addDate,ac.amount,op.supplier_ID --just櫃面where子句都不放過。