How to get rows with lowest value from a group by?

Discussion in 'MySQL' started by Mikkel Kjærgaard, Nov 1, 2021.

  1. Mikkel Kjærgaard

    Mikkel Kjærgaard New Member

    Joined:
    Oct 24, 2021
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Hi Experts
    I have a database of beverages, with following tables:
    Beverage (code, name, size) Primary key=code
    Store (name, area, phone) Primary key=name
    Sells (store_name, code, price) store_name refers to name(store) and code to code(beverage).

    I need to find the code and name of the least expensive beverages for each store (1 value per store), but I am only able to find minimum price per store without the code via:
    SELECT store_name, MIN(price) FROM Sells GROUP BY store_name ORDER BY min(price)

    This gives me the store name and price of the least expensive beverages, but I need to somehow combine this with the Beverage table, so I get code and name also.
    Can anyone help me?
     
  2. Richard To

    Richard To Member

    Joined:
    Sep 18, 2017
    Messages:
    87
    Likes Received:
    36
    Trophy Points:
    18
    Gender:
    Male
    something like this:

    Select Beverage.code, Beverage.name, store.name from Beverage, Store
    Where (code,store_name) in
    (Select code, store_name from Sells
    Where (store_name,price) in
    (SELECT store_name,MIN(price) FROM Sells GROUP BY store_name ))​
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice