So, I have a table and I wish to display the row with MIN price...
sellers - saleid, productsid[fk for products table], cid[fk for customers table], quantity
price.
Here are a few records:
sellers =1,2,2,200,5.00
sellers=2,3,4,100,1.00
I do this query:
select ProductID, Quantity, MIN(Price) FROM sellers
and I get this output
2,200,1.00
Why does it show the min Price however the first record column? It should display i.e. the corresponding row...
3,4,1.00
Any idea why this is?
=========EDIT======= Thanks for the suggestion guys it works. I have now another small problem. I wish to select the MIN price for each product. Is there a query I can use to do this? So, if for example this is the new sellers table:
sellers =1,2,2,200,5.00
sellers=2,3,4,100,1.00
seller=3,2,3,250,3.00
then the min price for products 2,3 would be
sellers=2,3,4,100,1.00
seller=3,2,3,250,3.00
How can I do something like this using the min function? I have tried
select c.Fname p.ProductName, s.ProductID, s.Quantity, s.Price
FROM sellers s, products p, customer c
WHERE s.Price = (select MIN(Price) FROM sellers WHERE p.ID=s.ProductID AND c.ID=s.cid);
This however does not seem to output the min price per unique product. Any ideas as to how I can rectify this?
Thanks
MIN()) when you also include the non-aggregated columns inGROUP BYclause.