How to calculate product prices cheaper than current row - sql

Need help with an SQL query.
I have a products table... Fields are...
product_id
product_name
product_price
I want to show all products and have an additional column that shows how many products are cheaper than the current product. So, for example....
product_id product_name product_price Products_cheaper
1 product 1 1.50 0
2 product 2 6.50 2
3 product 3 2.50 1
4 product 4 10.50 3
Any suggestions on this SQL query?
Using SQL Server. Also, if products are equal then it's 0.

SELECT product_id,
product_name,
product_price,
RANK() OVER (ORDER BY product_price) - 1 AS product_cheaper
FROM products
ORDER BY product_id
Result (I added the sample I used on my comment)
product_id product_name product_price product_cheaper
1 product 1 1.50 0
2 product 2 6.50 2
3 product 3 2.50 1
4 product 4 10.50 4
5 product 5 6.50 2

Related

Select the best selling product ID

What if I have table like this and I want to select the best selling product_id.
id
transaction_id
product_id
qty_sold
1
21
2
5
2
22
3
2
3
23
4
2
3
24
2
1
3
25
2
4
I want the best selling product_id with the highest qty_sold
Using SQLS, you can group by the productID, add up the number of sold, and order by the total descending. If we also take the minimum transaction ID per product, if two products come out to have the same total qty, we can take the minimum tran ID to split the tie
SELECT TOP 1 product_id, SUM(qty_sold) as sellcount, MIN(transaction_id) as firsttran
FROM t
GROUP BY product_id
ORDER BY SUM(qty_sold) DESC, MIN(transaction_id)
Once you're happy the sums are right etc, you can remove the , SUM(qty_sold) as sellcount, MIN(transaction_id) from the SELECT if you want/if you only need the prod ID

How to merge two rows and sum the columns

product
quantity
price
milk
3
10
bread
7
3
bread
5
2
And my output table should be
product
total_price
milk
30
bread
31
I can't seem to get my code to work. Here is my code
SELECT product, (SELECT (quantity*unit_price)
FROM shopping_history AS sh ) AS total_price
FROM shopping_history
GROUP BY product
You are looking for the aggregate function SUM (which doesn't require a sub-query) e.g.
SELECT product, SUM(quantity*unit_price) AS Total_Price
FROM shopping_history
GROUP BY product

Count all of a column where value is 2 and sum this value with price

I'm doing with Northwind database where I use the Products table. I need to count all of the rows where Category_Id is 2 and sum the amount with the prices.
Here's the example of a table shortly:
Category_ID | Unit Price
1 | 2,90
2 | 3,70
3 | 4,90
2 | 1,90
5 | 0,90
2 | 2,90
There are 3 rows where category_Id is 2. How to sum this 3 with that rows Unit price?
3,70 + 1,90 + 2,90 = 8,50
So the answer I need is 8,50 but I have no idea how to get that amount with a SQL query.
Does someone know?
you can get the aggregated values for all Ids using
Select Categeory_Id, sum([Unit Price]) Total, count(*) Qty
from Products
group by Category_Id
or just a specific total such as
select sum([Unit Price]) total
from products
where category_Id=2

SQL Query to FILTER customers who have purchased same product multiple times with a quantity greater than 5 [duplicate]

This question already has an answer here:
SQL Query to find out he customers who have purchased same product multiple times with a quantity greater than 5 [closed]
(1 answer)
Closed 2 years ago.
I have a table Customer_Order_Data with these columns:
customer_id
order_id
product_id
quantity
I want to find the customers who have purchased the same product (product_id) multiple times with a quantity > 5.
Let's say I have the following data -
Order_id
Customer_id
product_id
Quantity
00001
ABCD
B019
2
00002
ABCD
B019
6
00003
EFGH
B018
8
00004
ABCD
B019
7
00005
EFGH
B018
1
00006
IJKL
B017
9
00007
ABCD
B015
10
I want to filter out the customers who have purchased same product multiple times where quantity > 5.
Now Customer EFGH has purchased product B018 twice but do not have qauntity purchased greater than 5 in both cases, so it should not be displayed in result.
Result expected -
Order_id
Customer_id
product_id
Quantity
00001
ABCD
B019
2
00002
ABCD
B019
6
00004
ABCD
B019
7
One option uses a subquery:
select t.*
from mytable t
where (
select count(*)
from mytable t1
where
t1.customer_id = t.customer_id
and t1.product_id = t.product_id
and t1.quantity > 5
) > 1
The idea is to count how many rows in the table have the same customer and product, and a quantity greater than 5.
You can also use window functions:
select *
from (
select t.*,
sum(case when quantity > 5 then 1 else 0 end) over(partition by customer_id, product_id) as cnt
from mytable t
) t
where cnt > 1
You can use the IN clause as follows:
Select * from your_table
Where (customer_id, product_id) IN
(Select customer_id, product_id
From your_table t
Where quantity > 5
Group by customer_id, product_id
Having count(1) > 1)

TSQL Price Pivot Table

Im after some assistance on how to pivot the following data. I have a pricing table like the following example.a
It contains products with varying pricing information on Qty Breaks. What I need to do is take this data and pivot into the table. The database is hosted in MSSQL-2012
example.a
ProductId Qty Price Reference
---------+---+-----+-------------
3 1 18.92
3 1 4.65
3 24 3.72
3 72 3.45
3 1 3.40 Quote:11223344
---------+---+-----+-------------
Additionally:
When pivoting the table, if there are products with the same Qty, pick the lowest price. As Shown above, ProductID 3 has 3x rows of pricing where they have the same Qty. So the pivot must only select and show the lowest rate
Required Pivot Result
ProductId Qty1 Price1 Reference1 Qty2 Price2 Reference2 Qty2 Price2 Reference2
---------+----+------+---------------+----+------+--------------+----+------+-------------
3 1 3.4 Quote:11223344 24 3.72 72 3.45
---------+----+------+---------------+----+------+--------------+----+------+-------------
Really would appreciate some ideas on who to tackle such a Query
thanks