Exercise II using SQL left join, product stock report

There are three tables in total
  • plus2_product
  • p_id ,
    p_name
    price
  • plus2_product_receive
  • id
    p_id,
    product
    price ( Purchase Price)
    qty ( Quanity )
    dt ( date of purchase)
  • plus2_invoice_dtl
  • dtl_id,
    inv_id ( Invoice id)
    p_id (product id)
    product
    qty ( quantity sold)
    price( sale price)

LEFT JOIN ( Basic Query) Download SQL Dump of these three tables here.

Invoice generation by allowing products with minimum stock level based on procurement and sales data


From the above data we will produce following reports.
  1. Exercise on LEFT JOIN
  2. List of products received with total quantity
  3. Total of quantity sold and price against each product.
  4. List of Invoice with Total Price and Quanity against each invoice
  5. List of Sum of Prducts received, sum products sold, balance available stock against each product.
  6. Above list (report ) with products having stock more than 10 quantity.
  7. Above List of products with selling price.

1.List of products received with total quantity

SELECT a.p_id,a.product,SUM(a.qty) as receive 
FROM plus2_product_receive a GROUP BY a.p_id
p_idproductreceive
1Mouse10
2Key Board8
3Moniter14
4CPU16
5Pen Drive25
6Operating System8
7Power Unit8

2.Total of quantity sold and price against each product.

SELECT p_id,product,SUM(qty)AS Quantity,
FORMAT(SUM(price),2) AS Total_price FROM `plus2_invoice_dtl` GROUP BY p_id
p_idproductQuantityTotal_price
3Moniter220.45
4CPU676.35
5Pen Drive620.80
6Operating System310.23
7Power Unit612.36

3. List of Invoice with Total Price and Quanity against each invoice

SELECT inv_id,SUM(qty)AS Quantity_sold,FORMAT(SUM(price),2) AS Total_price 
FROM `plus2_invoice_dtl` GROUP BY inv_id;
inv_idQuantity_soldTotal_price
41380.66
5528.95
6530.58

4. List of Sum of Prducts received, sum products sold, balance available stock against each product.

COALESCE() to handle Null value
SELECT a.p_id,a.product,SUM(a.qty) as receive , COALESCE(b.sold,0) AS sold,
(SUM(a.qty) - COALESCE(b.sold,0)) AS stock FROM plus2_product_receive a
LEFT JOIN
(SELECT p_id,product,SUM(qty) AS  sold FROM `plus2_invoice_dtl` GROUP BY p_id) b 
ON a.p_id=b.p_id
GROUP BY a.p_id
p_idproductreceivesoldstock
1Mouse10010
2Key Board808
3Moniter14212
4CPU16610
5Pen Drive25619
6Operating System835
7Power Unit862

5.Above list (report ) with products having stock more than 10 quantity.

SELECT p_id,product,receive,sold,stock FROM (
SELECT a.p_id,a.product,SUM(a.qty) AS receive , COALESCE(b.sold,0) AS sold, 
(SUM(a.qty) - COALESCE(b.sold,0)) AS stock FROM plus2_product_receive a
LEFT JOIN
(SELECT p_id,product,SUM(qty) AS sold FROM `plus2_invoice_dtl` GROUP BY p_id) b  
ON a.p_id=b.p_id 
GROUP BY a.p_id
) AS t WHERE stock >10;
p_idproductreceivesoldstock
3Moniter14212
5Pen Drive25619

6.Above List of products with selling price.

SELECT t2.p_id,t2.p_name,receive,sold,stock,t2.price FROM (
SELECT a.p_id,a.product,SUM(a.qty) AS receive , COALESCE(b.sold,0) AS sold, 
(SUM(a.qty) - COALESCE(b.sold,0)) AS stock FROM plus2_product_receive a
LEFT JOIN
(SELECT p_id,product,SUM(qty) AS sold FROM `plus2_invoice_dtl` GROUP BY p_id) b 
 ON a.p_id=b.p_id
GROUP BY a.p_id) AS t1 
LEFT JOIN 
plus2_product as t2 on t1.p_id=t2.p_id 
WHERE stock >10
p_idproductreceivesoldstockprice
3Moniter1421220.45
5Pen Drive256198.50
LEFT JOIN ( Basic Query) Download SQL Dump of these three tables here.
Exercise I : Sales report from Product and sales tables .
SELECT Query LEFT JOIN using Multiple Tables
Exercise : Sales - Agent using table JOIN and Date functions
SQL commands
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com






    Post your comments , suggestion , error , requirements etc here





    SQL Video Tutorials










    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer