Select cheapest where sku or ean is even - mysql

I've a simple query like:
SELECT id, name, price, sku, ean
FROM products
WHERE id IN (1,2,3,4,5,6)
ORDER BY name ASC
Some products are double present (same sku or ean) with diffrent prices.
How to select the cheapest products where sku or ean is the same?

Related

How to SELECT based on grouped SUM that is compared to the AVG of that grouped SUM

I have a table consists of films, categories and prices.
Now I want to select only categories with total price (sum of all the price per film under that category) that is higher than the average of those categories' total prices.
I have been able to find the average of the total prices (thanks to this web) but can't combine it together.
Here are the queries:
-- Return the average sums of the price per category
SELECT AVG(sum_price)
FROM
(
SELECT category, sum(price) AS sum_price
FROM film_list
GROUP BY category
) AS inner_query;
-- Return category and its total price
SELECT category, SUM(price)
FROM film_list
GROUP BY category;
--[Error] Return only the category with sums of price larger than the average of sum of prices
SELECT category, SUM(price)
FROM film_list
WHERE SUM(price) >
(
SELECT AVG(sum_price)
FROM
(
SELECT category, sum(price) AS sum_price
FROM film_list
GROUP BY category
) AS inner_query
);
Any help will be much appreciated, thanks!
Try adding group by and then using having
SELECT category, SUM(price)
FROM film_list
GROUP BY category
HAVING SUM(price) >
(
SELECT AVG(sum_price)
FROM
(
SELECT category, sum(price) AS sum_price
FROM film_list
GROUP BY category
) AS inner_query
);
This is most easily solved using window functions:
SELECT c.*
FROM (SELECT category, SUM(price) AS sum_price,
AVG(SUM(price)) OVER () as avg_total_price
FROM film_list
GROUP BY category
) c
WHERE sum_price > avg_total_price

SELECT the customer who has ordered the greatest quantity of Products in the case of two customers

I have the following ORDERS table
I know query to select the customer that has ordered the greatest quantity. However, how would it work, if say, two customers have the same quantity. What query should I write to show both the customers?
You can use a subquery which checks that the quantity for a given record matches the largest quantity observed in the table:
SELECT *
FROM yourTable
WHERE qty = (SELECT MAX(qty) FROM yourTable)
This will return multiple records if there are more than one customer sharing the maximum quantity.
If you only wanted to get back a single record, even in the presence of ties, you could use this approach:
SELECT *
FROM yourTable
ORDER BY qty DESC
LIMIT 1
I think you want sum of qty per custNum.
If so you can try like:
select custNum,
sum(qty) as qty
from Orders
group by custNum
order by sum(qty) desc;
Fiddle here:
http://sqlfiddle.com/#!9/47931b/10
SELECT custnum,sum(qty) as total
FROM orders
group by custnum
having sum(qty) = (SELECT MAX(qty) FROM orders);
This will return both values.

group by, order by, distinct together

I have table with three columns (Trader, Product, Price). I want to get each Trader once and Product with max price to that Trader. When I select following I see repeated Traders:
select Trader, Product, Max(Price) as Price
from dbo.Sell
group by Trader, Product
order by Max(Price) desc
How can distinct Trader for the query above? The query below doesn't change anything:
select distinct Trader, Product, Max(Price) as Price
from dbo.Sell
group by Trader, Product
order by Max(Price) desc
Actually the following query does almost I want, however I want to see Product instead of Price.
select Trader, Max(Price) as Price
from dbo.Sell
group by Trader
Try this query, you would get multiple records only if a trader has more than one product with max price.
;with cte as (
select Trader, Max(Price) as Price
from dbo.Sell
group by Trader
)
select cte.Trader, s.Product, cte.Price
from cte join dbo.Sell s
on cte.Trader = s.Trader and cte.Price = s.Price
order by cte.Price desc
Second method would be to use a rank() function, but again rank() function will bring multiple records for same price records.
To get a single record, you can use Row_Number() in place of Rank().
;with cte as (
select Trader, Product, Price,
Rank() over (partition by Trader order by Price desc) rnk
from dbo.Sell
)
Select Trader, Product, Price
From cte
Where rnk = 1
Order by Price desc

compare AVG(integer) with AVG(integer) of a category

i have relation with three columns: ProductName, CategoryID and Price. i need to SELECT only those products which Price is higher than average product Price in given category.
(eg when apple(ProductName) is a fruit(CategoryID) it should be selected because its price is higher than average fruit price). How do i do that?
I'm thinking of something like this, but its obviously wrong since I'm comparing Price with Price and CategoryID:
SELECT ProductName, AVG(Price) FROM `products`
WHERE (SELECT AVG(Price)) > (SELECT CategoryID, AVG(Price))
GROUP BY ProductName;
Something like this
SELECT ProductName, Price, CategoryID FROM `products` p
WHERE Price > (SELECT AVG(Price) from `products` where CategoryID = p.CategoryID)

Not sure how to write a simple query

Hi I Have a table which has number of products and rating for each, due to some reasons I have different row of each product along with rate of each person who rated it, as following:
p1 2
p2 4
p3 4
p1 4
p1 5
p1 3
p2 7
I am using following query but it shows the average rate of all products, but I am expecting it to show the average rate of each product seperately.
Select ProductName, AVG(Rate) FROM Products
Just GROUP BY productName:
Select ProductName, AVG(Rate) AS 'Average Rate'
FROM Products
GROUP BY productName
Edit: To show the one with highest rate:
SELECT ProductName, Rate
FROM Products
ORDER BY rate DESC
LIMIT 1
Edit 2: To get the product name with the highest average rate:
SELECT productname, AVG(rate) Avgrate
FROM Products
GROUP BY productname
HAVING AVG(rate) =
(
SELECT AVG(rate)
FROM Products
GROUP BY productname
ORDER BY AVG(rate) DESC
LIMIT 1
)
You need to add Group By.
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName
To Sort Result use Order By after Group By
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName ORDER BY ProductName
Above query sort result in Ascending order if you want to sort descending then use DESC,
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName ORDER BY ProductName DESC
To Select Higher Rating Product
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName
HAVING AVG(Rate) = (SELECT MAX(AVG(Rate)) FROM Products GROUP BY ProductName)
This query return all product which have max average rating.
Only One Product with Higher rating use LIMIT in Descending Sort
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName
ORDER BY ProductName DESC
LIMIT 1