我有以下的輸入表:計算在多行
Article Store Supplier NetPrice Pieces Sum Inventory Price Cond
NL1234 N001 3100000 161,5 2 323 7 123,45 2,47
NL1234 N001 3100000 161,5 0 0 4 103,8 2,08
NL1234 N001 3100000 161,5 0 0 23 120,8 1,21
我需要計算價格的加權平均存貨價值的數量。 例如, 所有選定行的庫存*價格除以總數量。庫存number.Mathematically的,
((7 * 123.45)+(4 * 103.8)+(120.8))/(34)
SELECT
Article,
Store,
Supplier,
NetPrice,
sum(Pieces) as "Pieces",
sum(Sum) as "Sum",
sum(Inventory) as "Inventory",
(Inventory*Price)/sum(Inventory) as "Price",
(Inventory*Cond)/sum(Inventory) as "Cond"
FROM
table_name
WHERE
"Article" = 'NL1234'
GROUP BY
STORE,
SUPPLIER,
NetPrice,
Article
如何可以擴展/修改我的選擇聲明以獲得以下輸出:
Article Store Supplier NetPrice Pieces Sum Inventory Price Cond
NL1234 N001 3100000 161,5 2 323 34 119,35 1,57
MySQL或SQL服務器? – Mihai
任何事情,我只是無法得到這樣做的邏輯。如果你能幫助我,那麼我會非常感激 –
你的查詢應該已經產生了這個結果。你有什麼問題? – 2014-01-21 16:23:07