0

I'm trying to use MIN function in SQL, but I keep getting a lot of rows, and not just the MIN row.

I'm using this tutorial http://www.w3schools.com/SQl/trysql.asp?filename=trysql_func_min

running this query

SELECT 
    P.ProductID, 
    MIN(Price) AS SmallestOrderPrice 
FROM Products P 
GROUP BY ProductID;

I want to get just the row that has the min price from all prices, and it's corresponding product. If more than one product has the same price, I would like it to return both products.

4
  • what do you mean by I keep getting a lot of rows? you should get one row per product. if you have many products, you will obviously get many rows. Commented Dec 5, 2016 at 21:17
  • What are you expecting to see as a result? Do you only want the product with the lowest Price? Commented Dec 5, 2016 at 21:17
  • you should be getting the min price per product. eahc PRODUCTID returned should be uniq, and you're getting the min of each product price. if you just want the MIN price and know what product it is, you need a different query. Commented Dec 5, 2016 at 21:17
  • Ah i see. so how do i get the min price from all the rows? and return it's productID? Commented Dec 5, 2016 at 21:18

3 Answers 3

2

Perhaps you want the productid and the min price across all products.

The window function way.

SELECT *
FROM (SELECT P.ProductID, Price AS SmallestOrderPrice, row_number() over (order by price asc ) RN
      FROM Products P)
WHERE RN = 1  

Or my old standard... (Generally works across all platforms and useful if you need data from the 2nd table, you don't appear to)

SELECT * 
FROM  products P
INNER JOIN  (Select min(price) mp from products) P2
 on P.Price = P2.MP

or using the exists method... (Generally Compliant across all DBMS and likely fastest)

   SELECT P.* 
   FROM  products P
   WHERE EXISTS (SELECT min(price) mp 
               FROM products P2 
               WHERE P2.Price = P.Price)

I'm not sure if the having would work but it seems like it should

SELECT 
    P.ProductID, 
    MIN(Price) AS SmallestOrderPrice 
FROM Products P 
GROUP BY ProductID
HAVING min(Price) = price
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, one of your ways did it!
2

Below is your query, with TOP 1 and ORDER BY Price ASC added.

The ORDER BY will cause your current returns to be ordered in ASCending order, putting the smallest price at the top. Then the TOP 1 will limit it to just that smallest price.

SELECT TOP 1
    P.ProductID, 
    MIN(Price) AS SmallestOrderPrice 
FROM Products P 
GROUP BY ProductID
ORDER BY Price ASC;

Note, if there are multiple products with the same minimum price, this won't return all of them. In that case, xQbert's queries are probably best.

Comments

0

Min Price for all the rows.

    SELECT MIN(Price)
    FROM Products

And if you want ALL the ID also

SELECT
p.ProductID
, mp.MinPrice AS SmallestOrderPrice
FROM
    Products P
    CROSS APPLY (SELECT MIN(Price) MinPrice FROM Products ) AS mp
WHERE
    p.Price = mp.MinPrice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.