Get stock from Purchased and Sold Products QTY - sql

I have been working on Client project. I need to get stock from Purchased product QTY and Sold QTY.
Here my SQL
SELECT DISTINCTROW tbl_itemmaster.product_code,tbl_itemmaster.unittype,tbl_itemmaster.color,SUM(tbl_purchasedetails.qty),SUM(tbl_saledetails.qty),(SUM(tbl_purchasedetails.qty)-SUM(tbl_saledetails.qty))
FROM (tbl_itemmaster LEFT JOIN tbl_saledetails ON tbl_itemmaster.[product_code] = tbl_saledetails.[product_code])
LEFT JOIN tbl_purchasedetails ON tbl_itemmaster.[product_code]=tbl_purchasedetails.[product_code] GROUP BY tbl_itemmaster.product_code,tbl_itemmaster.unittype,tbl_itemmaster.color
Below is the result of sql
-----------------------------------------------------------------
Product Code Unit Type Color Purchase QTY Sold QTY Stock
-----------------------------------------------------------------
1001 KiloGram Red 500
500 Kilogram White 1033 300 733
570 Kilogram Black
600 Kilogram Pink
9005 Kilogram Magenta 800.5
900 Kilogram Green
-----------------------------------------------------------------
Product code 500 has three times purchase entry i.e. 500+400+133 = 1033 Purchased Qty
Product code 500 has only ONE time sale entry i.e. 100 = 100 Sold Qty
Product code 500 suppose to has 933 Stock Qty
BUT why I getting 300 in sold qty ?
Where my query is lacking?
PLEASE HELP !!!

You are getting cartesian products, because products have multiple sales and purchases.
The solution is to do the aggregation before the join:
SELECT im.product_code, im.unittype, im.color,
pd purchaseqty, sd.salesqty),
(purchaseqty - salesqty)
FROM (tbl_itemmaster as im LEFT JOIN
(SELECT product_code, SUM(qty) as salesqty
FROM tbl_saledetails
GROUP BY product_code
) as sd
ON im.[product_code] = sd.[product_code]
) LEFT JOIN
(SELECT product_code, SUM(qty) as purchaseqty
FROM tbl_purchasedetails
GROUP BY product_code
) as pd
ON im.[product_code] = pd.[product_code];

Related

Find the products contributing to the 50% of the total sales using SQL SUM window Function

There are two Tables - orders and item_line
orders
order_id
created_at
total_amount
123
2022-11-11 13:40:50
450.00
124
2022-10-30 00:40:50
1500.00
item_line
order_id
product_id
product_name
quantity
unit_price
123
a1b
milo
4
100.00
123
c2d
coke
5
10.00
124
c2d
coke
150
10.00
The question is:
Find the products contributing to the 50% of the total sales.
My take on this is -
SELECT i.product_name,SUM(o.total_amount)AS 'Net Sales'
FROM item_line i
JOIN orders o on o.order_id = i.order_id
GROUP BY i.product_name
HAVING SUM(o.total_amount) = (SUM(o.total_amount)*0.5);
But this is not correct. SUM windows functions need to be used, but how?
Try the following, explanation is within the query comments:
-- find the the total sales for each product
WITH product_sales AS
(
SELECT product_id, product_name,
SUM(quantity * unit_price) AS product_tot_sales
FROM item_line
GROUP BY product_id, product_name
),
-- find the running sales percentage for each product starting from porduct with highest sales value
running_percentage AS
(
SELECT product_id, product_name, product_tot_sales,
SUM(product_tot_sales) OVER (ORDER BY product_tot_sales DESC) /
SUM(product_tot_sales) OVER () AS running_sales_percentage,
SUM(product_tot_sales) OVER () AS tot_sales
FROM product_sales
)
-- select products that have a running sales percentage less than the min(running_sales_percentage) where running_sales_percentage >= 0.5
-- this will select all of products that contributes of 0.5 of the total sales
SELECT product_id, product_name, product_tot_sales,
tot_sales,
running_sales_percentage
FROM running_percentage
WHERE running_sales_percentage <=
(
SELECT MIN(running_sales_percentage)
FROM running_percentage
WHERE running_sales_percentage >= 0.5
)
You don't need a join with orders table, all data you need is existed in the item_line table.
See demo.

How to calculate benefit of some specific product type in SQL Server?

So, here are my tables:
Sales
id
product_code
1
4536
2
4674
Products
product_code
product_name
price
real_price
4536
Red bull energy drink 300 ml
3,68
2,88
4674
Mac coffee 25 gr
2,59
2,10
I need to calculate how much benefit did I get from the products which have "Red Bull" in its name. Benefit is equal to price-real_price.
Expected output:
product_name
benefit
Red bull energy drink 300 ml
4536,4
Here is what I tried:
SELECT products.product_code,(price-real_price) as profit
FROM products
INNER JOIN sales
ON products.product_code = sales.product_code
outer apply (select count(*)
from sales as benefit
where product_name like '%red bull';
But it does not give me the output I want to get.
create table Sales (
id int,
product_code int)
create table Products(
product_code int,
product_name char(100),
price smallmoney,
real_price smallmoney)
insert into Sales values
(1, 4536),
(2, 4674),
(3, 4536) -- to have multiple sales for Red Bull
insert into Products values
(4536, 'Red bull energy drink 300 ml', 3.68, 2.88),
(4674, 'Mac coffee 25 gr', 2.59, 2.10)
select
p.product_name, sum(p.price - p.real_price) benefit
from Sales s join Products p on s.product_code = p.product_code
group by p.product_name
product_name
benefit
Mac coffee 25 gr
0.4900
Red bull energy drink 300 ml
1.6000
As you say that each line is one quantity sold the following query will do the job.
I suggest that it would be better to have a quantity column in the sales table.
select
p.product_code,
p.product_name,
count(s.product_code) as quantity_sold,
sum( (p.price-p.real_price)*count(s.product_code) as total_profit
from products p
join sales s on s.product_code = p.product_code
where p.product_name like '%red bull'
group by
p.product_code,
p.product_name;

I want a query to extract the table data from SQL Server

Items table:
ItemNo, Description, BaseUnitOfMeasure, UnitPrice
-------------------------------------------------
1 Prod1 pc 200
2 prod2 pc 150
3 prod3 pc 120
ItemsLedger table:
ItemNo, Quantity
----------------
1 400
1 -20
1 -5
Above are my two tables and I want to extract the data in a format below
ItemNo, Description, BaseUnitOfMeasure, UnitPrice, Quantity
------------------------------------------------------------
1 prod1 pc 200 375
I tried this query but didn't work
SELECT
item.[No_],
item.[Description],
item.[Base Unit of Measure],
item.[Unit Price],
SUM(ledger.[Quantity])
FROM
items AS item
LEFT JOIN
ItemLedger AS ledger ON item.No_ = ledger.[Item No_]
GROUP BY
item.No_
but it didn't work. Ignore typos and table names etc.
In SQL Server, you need to include all non-aggregated columns in the GROUP BY:
SELECT i.[No_], i.[Description], i.[Base Unit of Measure], i.[Unit Price],
Sum(il.[Quantity])
FROM items i LEFT JOIN
ItemLedger as il
ON i.No_ = l.[Item No_]
GROUP BY i.[No_], i.[Description], i.[Base Unit of Measure], i.[Unit Price]

stock query not show exact result

I use this query to maintain my stock but its not show exact result
select p.Product_Name Product
,isnull(sum(d.qty),0) Purchase_Qty
,isnull(sum(i.qty),0) Issue_Qty
,(isnull(sum(d.qty),0)-isnull(sum(i.qty),0)) InStock
from purchase_dtl d
left join issue_dpt i on i.Product_ID=d.Product_ID
right join product p on p.id = d.Product_ID
group by Product_Name
Query result
Product | Purchase_Qty | Issue_Qty | InStock
---------------------------------------------
Pen 1000 300 700
Books 4000 1000 3000
My exact purchase qty
Product | Purchase_Qty
----------------------
Pen 500
Books 2000
My exact issue qty
Product | Issue_Qty
-------------------
Pen 300
Books 1000
Please help me to solve my problem
Thanks
select p.Product_Name Product
, p.Purchase_Qty
, isnull(sum(i.qty),0) Issue_Qty
, p.Purchase_Qty-isnull(sum(i.qty),0) InStock
from (select p.id, p.Product_Name, isnull(sum(d.qty),0) Purchase_Qty
from product p left join purchase_dtl d on p.id = d.Product_ID
group by p.id, p.Product_Name) p
left join issue_dpt i on i.Product_ID = p.id
group by Product_Name, p.Purchase_Qty
I suppose joining purchase_dtl and issue_dpt gives you some extra rows for Purchase_Qty
Join product with purchase_dtl first to calculate the aggregate data and then join again with issue_dpt

I need product Stock quantity from two tables

I have two tables:
product_in (store all product QTY)
Product Code(PK) Description QTY
RS121102 SUITS 100
RS129985 SUITS 100
DF-C09 SHIRTS 50
AE-H05 SHIRTS 50
product_out (store all products sold QTY)
Product Code Description QTY
RS121102 SUITS 50
AE-H05 SHIRTS 10
I want result like below
Product Code Description Total Qty Sold QTY
RS121102 SUITS 100 50
RS129985 SUITS 100 0
DF-C09 SHIRTS 50 0
AE-H05 SHIRTS 50 10
How can I do this?
SELECT pi.ProductCode, pi.Description, pi.QTY AS TotalQty,
ISNULL(po.QTY, 0) AS SoldQty
FROM product_in as pi
LEFT JOIN product_out as po
ON po.ProductCode = pi.ProductCode
That's asuming there aren't multiple records for each product in product_out.
Try this:
SELECT ProductCode, Description, SUM(Total_QTY) AS Total_Qty, SUM(Sold_Qty) AS Sold_Qty
FROM
(
SELECT ProductCode, Description, QTY As Total_Qty, 0 As Sold_Qty
from product_in
)
UNION ALL
(
SELECT ProductCode, Description, -QTY As Total_Qty, QTY As Sold_Qty
from product_out
)
GROUP BY ProductCode, Description