Select rows with column with min value

10,044

This will accomplish that:

SELECT t1.*
FROM yourTable t1
  LEFT JOIN yourTable t2
    ON (t1.OrganisationID = t2.OrganisationID AND t1.Distance > t2.Distance)
WHERE t2.OrganisationID IS NULL;

sqlfiddle demo

Note that if there are multiple rows with the lowest distance duplicate, this returns them both


EDIT:

If, as you say in the comments, only want one column and the MIN distance you can do it easily with MIN and GROUP BY:

SELECT city, MIN(distance)
FROM table2
GROUP BY city;

sqlfiddle demo

p.s. i saw your previous question that you deleted, and was answering it with a different thing than this (was going to tell you that since you had the organisationID in the WHERE clause, you could just do: SELECT TOP 1 ... order by Distance DESC), but if you need more it for more than one organisationID, this is something that can get you there)

Share:
10,044

Related videos on Youtube

Johann
Author by

Johann

Medium Articles Creating responsive layouts with Jetpack Compose https://johannblake.medium.com/creating-responsive-layouts-using-jetpack-compose-7746ba42666c Create Bitmaps From Jetpack Composables https://proandroiddev.com/create-bitmaps-from-jetpack-composables-bdb2c95db51 Navigation with Animated Transitions Using Jetpack Compose https://proandroiddev.com/navigation-with-animated-transitions-using-jetpack-compose-daeb00d4fb45 In-App Testing For Android https://proandroiddev.com/in-app-testing-for-android-6f762bb97387 Github https://github.com/JohannBlake ANDROID DEVELOPMENT 10 years of native Android development with Kotlin, Java & Android Studio Design, develop, test and deploy Android applications Professionally looking UIs with Jetpack Compose & Material Design Develop using continuous integration APIs: Google Cloud Messaging, Google Maps, Google Drive, Gmail, OAuth Frameworks & Patterns: MVVM, RxJava, Dagger, Koin, Retrofit, Sqlite, Room, Realm, Crashlytics Communicate with web services via RESTful APIs Troubleshoot, optimize and performance tune Code versioning using Git and the Gitflow model Project management with Jira Agile development with Scrum WEB DEVELOPMENT 25 years of web development React, Material-UI Javascript, HTML5, CSS3, jQuery, ASP.NET, C#, Java, Servlets Microservices running on the Google Cloud Platform ELECTRON DEVELOPMENT 2 Years of Electron development Javascript, jQuery Node.js HTML5, CSS3 Published Apps Motel One https://play.google.com/store/apps/details?id=com.motelone.m NBC Sports https://play.google.com/store/apps/details?id=air.com.nbcuni.com.nbcsports.liveextra Telemundo Noticias https://play.google.com/store/apps/details?id=com.nbcuni.telemundo.noticiastelemundo TD Mobile Banking https://play.google.com/store/apps/details?id=com.td

Updated on September 15, 2022

Comments

  • Johann
    Johann almost 2 years

    I need to select the rows with the minimum distance by grouping on the OrganisationID. Here is my data in a single table:

    ID  OrganisationID    Distance
    0        10             100
    1        10             200
    3        10             50
    4        20             80
    5        20             300
    

    This is the result I want:

    ID  OrganisationID    Distance
    3        10             50
    4        20             80
    
  • Johann
    Johann over 10 years
    So it's not possible to do this with the Group By clause and MIN function?
  • Raphaël Althaus
    Raphaël Althaus over 10 years
    @AndroidDev not directly if you need the Id.
  • Filipe Silva
    Filipe Silva over 10 years
    @AndroidDev. What Raphael said :)
  • Johann
    Johann over 10 years
    Actually, I have a 4th column called City. I don't really need the ID. Is it possible to return just the Distance and City? I don't even need to return the OrganisationID.
  • Filipe Silva
    Filipe Silva over 10 years
    @AndroidDev. if you only need city and MIN(distance) you can do the MIN(distance) and GROUP BY city. This kind of tricks (mine or Raphael's) is to get more than one column besides the aggregated column. If you only need one, you can do the MIN and group by
  • Johann
    Johann over 10 years
    The problem with Group By City is that I can have two different cities belonging to the same organistion and this will result in both cities being returned. I only want the one that has the smallest distance.
  • Filipe Silva
    Filipe Silva over 10 years
    @AndroidDev. Yes. That is true. You gotta use the workaround then.