I am working on The mySQL database. Here is my DB-Fiddle. Need output like below.
I have used the below query
select
a.product_name as 'Product Name',
a.product_model 'Product Model',
c.batch_id as 'Batch',
b.rate as 'Sale Price',
c.supplier_rate as 'Purchase Price',
sum(b.quantity) as 'IN-QTY',
sum(c.quantity) as 'OUT-QTY',
(sum(b.quantity)-sum(c.quantity)) as 'Stock',
(sum(b.quantity)-sum(c.quantity)*b.rate) as 'Stock Sale Price',
(sum(b.quantity)-sum(c.quantity)*c.supplier_rate) as 'Stock Purchase Price'
from
product_information a
left join product_purchase_details b on a.product_id = b.product_id
left join invoice_details c on a.product_id = c.product_id
GROUP BY
b.batch_id
Output
In the above image the In-QTY, Out-Qty, Stock, Stock Sale Price and Stock Purchase Price is wrong.
Update 1
The fiddle has 5 tables and from the below information I have tried to carry out my desired result
- Product Name and Product Model from product_infortmation table
- Batch from invoice_details or product_purchase_details
- Sale price is the price entered at the time of purchase. So it's the
ratefrom product_purchase_details table - Purchase price is the
supplier_ratetaken from invoice_details table - In-Qty is the
quantityat the time of purchase and taken from product_purchase_details table - Out-Qty is the
quantityat the time of invoice taken from the invoice_details table. - Stock is In-QTY - Out-Qty
- Stock Sale Price is Stock * Sale Price
- Stock Purchase Price is IN-QTY * Purchase Price
Update 2 Updated The Fiddle
Select Product_Name,Product_Model,Batch,Sale_Price,Purchase_Price,IN_QTY,OUT_QTY, (IN_QTY-OUT_QTY) as STOCK,((IN_QTY-OUT_QTY)*Sale_Price)as Stock_Sale_Price, IN_QTY*Purchase_Price as Stock_Purchase_Price
from
(SELECT b.product_name as 'Product_Name',b.product_model as 'Product_Model',a.batch_id as 'Batch',sum(a.quantity) as 'IN_QTY',a.rate as 'Purchase_Price',0 as 'Sale_Price',
0 as 'OUT_QTY'
FROM `product_purchase_details` a
inner join product_information b on a.product_id = b.product_id
GROUP by a.batch_id
UNION ALL
SELECT 0 as 'Product_Name',0 as 'Product_Model',0 as 'Batch',0 as 'IN_QTY',0 as 'Purchase_Price',a.supplier_rate as 'Sale_Price',sum(a.quantity) as 'OUT_QTY'
FROM invoice_details a
GROUP by a.batch_id
)z
group by Batch
Output
Again the output is not what I desired.
Note There can be no sale of any particular batch.
Any help would be highly appreciated



The SUMs are too large, correct?
When
JOINingand doingSUMs(orCOUNTs, etc), please note that theJOINis done first. This generates an intermediate file containing all relevant combinations of all the Joined tables. Then theSUMs(etc) are computed.If a
SUMshould be against less than the Join of all the tables, then it needs to be computed in a subquery. Depending on the situation, it might be done in either of these ways:Or
The first one usually works well if the
SUMis coming from a single table, independent of the other tables.The second is often better if you need to compute multiple
SUMsin one table before combining with the other tables.In either reformulation, it may be valid to have a
JOINin the subquery -- perhaps 2 tables are needed to get the correct tally.