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

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

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;

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

MS-Access Get price of product at certain order date

I have a table filled with purchase prices, like this:
sku price btw startdate
PCR-CA5425023181515 21,17 € 1 01/01/2009
PCR-CA5425023181515 999,00 € 1 06/06/2009
PCR-CA5425023181515 444,00 € 4 09/07/2009
PCR-CA5425023181515 100,00 € 4 10/08/2009
I have another table filled with orders, like this:
sku quantity orderdate
PCR-CA5425023181515 5 01/05/2009
PCR-CA5425023181515 10 01/12/2009
PCR-CA5425023181515 10 24/12/2009
My goal is to get every purchase price per order from that date.
(For example: when I ordered the product on the first of may (01/05) it cost 21,17 euros.
When I ordered it on the first of december (01/12) it cost 100,00 euros.)
I've been struggling with this for the past hour, but haven't found anything useful yet.
SELECT
O.sku,
O.qty,
PP.price
FROM
Orders O
INNER JOIN Purchase_Prices PP ON
PP.sku = O.sku AND
PP.start_date <= O.order_date
WHERE
NOT EXISTS
(
SELECT
*
FROM
Purchase_Prices PP2
WHERE
PP2.sku = PP.sku AND
PP2.start_date <= O.order_date AND
PP2.start_date > PP.start_date
)
Alternatively:
SELECT
O.sku,
O.qty,
PP.price
FROM
Orders O
INNER JOIN Purchase_Prices PP ON
PP.sku = O.sku AND
PP.start_date <= O.order_date
LEFT OUTER JOIN Purchase_Prices PP2 ON
PP2.sku = O.sku AND
PP2.start_date <= O.order_date AND
PP2.start_date > PP.start_date
WHERE
PP2.sku IS NULL