I need product Stock quantity from two tables - sql

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

Related

Query to find 2 cheapest items sum value of which gives 3000

I have a table Products:
ProductName Price
straight jeans 1500
slim jeans 2500
Denim jacket 3000
Denim shorts 800
Skinny jeans 1700
loose Jeans 2100
mom Jeans 2800
wide jeans 1850
distressed jeans 1100
bootcut jeans 1350
For purchased two different things with a total value of 3000 or more, they give a third as a gift.
I need a SQL query to spend minimum on two things, and take third as the most expensive.
The only thing I've come up with is to go through all possible combinations and find the cheapest combination that over 3000.
WITH Products_sum AS (
SELECT p1.ProductName AS ProductName1, p2.ProductName AS ProductName2, p1.Price + p2.Price AS TotalPrice
FROM products p1
JOIN products p2
ON p1.ProductName < p2.ProductName
)
SELECT top 1 ProductName1, ProductName2, TotalPrice
FROM Products_sum
WHERE TotalPrice >= 3000
order by TotalPrice asc
I'm expecting answer like:
bootcut jeans 1350
Skinny jeans 1700
Denim jacket 3000
But don't know how to do exactly like that.
I continued from where you left it and compose the expected result.
Converted as cte the top pair of products and the most expensive.
Combine the final resultset with union statement.
WITH Products_sum AS (
SELECT p1.ProductName AS ProductName1
, p2.ProductName AS ProductName2
, p1.Price + p2.Price AS TotalPrice
, p1.Price Price1
, p2.Price Price2
FROM products p1
JOIN products p2
ON p1.ProductName < p2.ProductName
),
topProducts as (SELECT top 1 ProductName1, ProductName2, Price1, Price2
FROM Products_sum
WHERE TotalPrice >= 3000
order by TotalPrice asc),
moreExpensive as (
select top 1 ProductName, Price
from products
order by price desc
)
select productName, Price from(
select 1 as pos, ProductName1 ProductName, Price1 Price from topProducts
union
select 2, ProductName2 , Price2 from topProducts
union
select 3, ProductName, Price
from moreExpensive )q
order by pos
SELECT p1.ProductName, p1.Price,
p2.ProductName, p2.Price,
p3.ProductName, p3.Price
FROM Products p1
JOIN Products p2
on p1.ProductName < p2.ProductName
JOIN Products p3
ON p2.ProductName < p3.ProductName AND
p1.Price + p2.Price + p3.Price > 3000
LEFT JOIN Products nonexistent
ON NOT (nonexistent.ProductName IN (p1.ProductName, p2.ProductName, p3.ProductName)) AND
p1.Price + p2.Price + nonexistent.Price > 3000 AND
nonexistent.Price < p3.Price
WHERE nonexistent.ProductName IS NULL
We
select every (p1, p2, p3) tuples
where their name differs
their price is greater than 3000
and there doesn't exist any nonexistent record that would differ from these three, would yield a smaller price with p1 and p2 than p3 but still above 3000

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;

SQL summary with highest price and total qty

have the following table and data
partno price qty
A0001 10 2
A0001 8 6
A0001 15 10
How can I issue a query to get the following result
partno. price. qty.
A0001 15 250
unique partno, highest price in the list and sum(qty )* highest price.
A simple aggregation shows the result you want:
select
partno,
max(price) as max_price,
max(price) * sum(qty) as total
from t
group by partno
/*Just replace "Table_Name" with your first table's name in the database */
select
distinct
partno,
max(price) price,
max(price) * sum(qty) qty
from
Table_Name
group by
partno

Get stock from Purchased and Sold Products QTY

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];