I have a table product with sample data as below
product 1 2 2 3 1
Quantity 10 15 20 10 5
I need to findout which product has highest quantity after summing up all the available quantity for all the products
Example: Here answer would be 15+20 = 35 is greatest so product 2 has more quantity
please help with query
select product, sum(quantity) as sum_quantity
from products
group by product
order by sum_quantity desc
limit 1
Related
I have a table with product_id, warehouse, quantity, and price. My goal is to select the lowest price for each product. However, I want to choose the record with the lowest price that has a quantity, unless they are all 0 in which case I want the record with just the lowest price.
Example
product_id
warehouse
quantity
price
1
A
5
14
1
B
0
12
1
C
8
17
2
A
0
12
2
C
0
10
3
D
3
12
After the query, it should return a table such as this:
product_id
warehouse
quantity
price
1
A
5
14
2
C
0
10
3
D
3
12
My initial thought is to do some kind of inner join on price = min(price). I know how to get the record with the min price but can't wrap my brain around having it dependent on the quantity as well.
I've been trying to get MIN of SUM of a column by doing subqueries, however in a case where I have two same SUM values, my query only returns me one of them. Is there a way to get all of them to be shown?
So for example this table is called quantity,
date product_id quantity_start quantity_end
1/1/2020 1 10 5
1/1/2020 2 10 5
1/1/2020 3 12 1
2/2/2020 1 10 5
2/2/2020 2 11 6
2/2/2020 3 14 1
my query would be
SELECT product_id, MIN(Total) as Minimum
FROM (SELECT product_id, SUM(quantity_start - quantity_end) as Total
FROM quantity
GROUP BY product_id)T
But this will return me only one min value while there are two since both product 1 and 2 will have total of 10 from my subquery. Is there a way to write it such that it shows me both ?
Thanks!
You are correctly grouping by your sub query, but the outer query also needs a group by so:
SELECT product_id, MIN(Total) as Minimum
FROM (SELECT product_id, SUM(quantity_start - quantity_end) as Total
FROM quantity
GROUP BY product_id)T
GROUP BY product_id;
See it working here: http://sqlfiddle.com/#!9/97d7724/1
I have a Products table with three headers namely ProductID, ProductName and Price.I need to find the name of the product which is having maximum average price. There are multiple products and they are duplicate as well for different product ID and i need their average price.
Sample Data looks like below :
ProductID ProductName Price
1 A 18
2 A 19
3 B 10
4 C 22
5 D 21.35
6 C 25
7 A 30
8 B 40
9 E 97
10 A 31
Query that i have written is as below:
SELECT ProductName
FROM Products
group by ProductName
order by avg(Price) desc
limit 1;
But i was informed that it is not a generic solution. Can anyone please inform, what is wrong in this query.
The only problem with your query is the case where there are more than 1 products with average price equal to the maximum average price, because it returns only 1 row.
To cover this case you need to group again and use your query in the HAVING clause :
select productname
from products
group by productname
having avg(price) = (
select avg(price)
from products
group by productname
order by avg(price) desc
limit 1
);
See the demo.
I have a pricing lookup table in MySQL where I need to lookup the right pricing based on the transaction quantity.
Say for example, i have a pricing table pricing, looks like:
product quantity price
prod1 1 4
prod1 10 3
prod1 100 2
prod1 1000 1
prod2 1 0.4
...
And I have a table called transaction where contains the sales data:
product sales
prod1 144
prod2 2
...
How can I get the sales multiply by the right unit price based on the quantity.
Something likes:
product sales quantity unitPrice
prod1 144 100 2
prod2....
I tried to join two table on product but don't know where to go from there.
One way to get the price is using a correlated subquery:
select t.*,
(select p.price
from pricing p
where p.product = t.product and p.quantity >= t.quantity
order by p.quantity
limit 1
) as price
from transaction t;
A similar subquery can be used to get other information such as the pricing tier.
For performance, you want an index on pricing(product, quantity).
there are two tables one is order and 2nd one is order_details respectively,
order table
order_id order_name
1 shoes
2 wallet
3 socks
4 bats
order_details table
order_details_no order_id(foregin key) order_price
1 1 25
2 1 55
3 2 65
4 4 30
5 4 60
My question is, I want result set which includes order_id, order total price in ascending order (eg order 1 total is 80,order 4 total is 90 )
How to get this ?
select order_id,
sum(order_price) as total_sum
from order_details
group by order_id
order by total_sum asc
Select
order.order_name,
sum(order_details.order_price) as price
from order
join order_details
on order_details.order_id=order.order_id
group by
order.order_id
order by
price desc