In my SQLite I have the following tables:
Products:
Pro_id. Pro_name
1. Item1
2. Item2
3. Item3
4. Item4
Purchases:
Pro_id. Pur_Qty Pur_Price
1. 10. 10
1. 10. 11
2. 20. 10
2. 10. 12
.. .. ..
Sales:
Pro_id. Sa_Qty. Sa_price
1. 10. 15
1. 10. 20
2. 20. 15
2. 10. 25
Now i want:
1- to show product ID and product name from products.
2- i want to sum avg prices from both tables sales & purchases and show it.
3- i want to sum profits for all products every product must have it's profits ..
How to do that?
I tried this :
select
pro_name,
sum(sa_qty)* (sum(sa_qty*sa_price)/sum(sa_qty)) -
sum(sa_qty)*(sum(pur_qty*pur_price)/sum(pur_qty)) as Profits
from products
left join purchases on purchases.pro_id= products.pro_id
left join sales on sales.pro_id = products.pro_id
group by pro_name
having sales.pro_id