so i have a table with products
Product ID | Product Name
===========+===============
1 | Tissues
2 | Glass
I have a table with sales
Sale ID | Product ID | Quantity | Price
===========+============+==========+=============
1 | 1 | 1 | 55
2 | 2 | 1 | 60
and i have a table of purchases
Batch ID | Total Value | Quantity | Product ID
=========+=============+==========+==================
1 | 100 | 100 | 1
2 | 10 | 50 | 2
3 | 1 | 1 | 2
So im trying to calculate the profit based on average cost using the query
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`
But i can't seem to get it to work i get a 'average price' is an unknown column, how would I structure the query correctly