,所以我有產品表相關子查詢的MySQL
Product ID | Product Name
===========+===============
1 | Tissues
2 | Glass
我有銷售
Sale ID | Product ID | Quantity | Price
===========+============+==========+=============
1 | 1 | 1 | 55
2 | 2 | 1 | 60
一個表,我有購買
Batch ID | Total Value | Quantity | Product ID
=========+=============+==========+==================
1 | 100 | 100 | 1
2 | 10 | 50 | 2
3 | 1 | 1 | 2
所以我嘗試的表根據平均成本計算利潤使用查詢
SELECT tblsale.product_id,
tblproduct.product_name,
SUM(tblsale.`quantity`) qty,
SUM(tblsale.`Price`*tblsale.`quantity`) sales,
(SELECT sum(total_value)/sum(quantity) VWAP
FROM tblpurchases
WHERE product_id = tblsale.product_id) average_price,
(average_price * qty) cost,
(sales-cost) profit
FROM tblsale, tblproduct
WHERE tblproduct.product_id = tblsale.`product_id`
GROUP by tblsale.`product_id`
但我似乎無法得到它的工作,我收到了「平均價」是一個未知的專欄中,我將如何構建查詢正確
什麼是「總價值」和「數量」? – 2012-02-12 16:39:55
總價值是所有商品在採購中的價值,數量是商品的單位數量(所以價值/數量)將是該特定批次的平均價格 – Akshat 2012-02-12 19:23:49