,所以我有产品表相关子查询的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