I made a question earlier but since I have deleted it because it was brought to my attention that there was not enough details shown. I am back with details regarding the problem I am having.
The problem is more to do with "HOW" do I construct the query.
Here are some details regarding my problem.
TABLE: orders
`orders_id`
TABLE: orders_products
`orders_products_id`
`orders_id`
`products_id`
TABLE: products
`products_id`
`manufacturers_id`
`products_price`
`products_cost`
TABLE: manufacturers
`manufacturers_id`
What I need outputting is;
For each manufacturer_id
, the sum of all the products_price
, the sum of all the products_cost
and then the products_profit
, derived from them both (aka price - cost = profit).
What I did have was:
SELECT m.manufacturers_id, m.manufacturers_name,
SUM(p.products_price1) as 'brand_sales',
SUM(p.products_cost) as 'brand_cost'
FROM orders o
LEFT JOIN orders_products op
ON o.orders_id = op.orders_id
LEFT JOIN products p
ON op.products_id = p.products_id
LEFT JOIN manufacturers m
ON p.manufacturers_id = m.manufacturers_id
GROUP BY m.manufacturers_name
And this did, sort, of what I wanted, albeit incorrectly. It did pull back the necessary data but the price, cost and thus the profit fields were populated by values seemingly about 15 times greater than expected and I have no idea what to do about it. My knowledge of SQL is growing but this problem needs a solution soon (and my job is sort of dependant on such a solution being found).
Thanks in advance guys, really really appreciate any adivce/help/comments.