Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 753865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:56:46+00:00 2026-05-14T14:56:46+00:00

Is there a way to select records based using an if statement? My table

  • 0

Is there a way to select records based using an if statement?

My table looks like this:

id | num | dis 
1  | 4   | 0.5234333
2  | 4   | 8.2234
3  | 8   | 2.3325
4  | 8   | 1.4553
5  | 4   | 3.43324

And I want to select the num and dis where dis is the lowest number… So, a query that will produce the following results:

id | num | dis 
1  | 4   | 0.5234333
4  | 8   | 1.4553
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:56:47+00:00Added an answer on May 14, 2026 at 2:56 pm

    If you want all the rows with the minimum value within the group:

    SELECT id, num, dis
    FROM table1 T1
    WHERE dis = (SELECT MIN(dis) FROM table1 T2 WHERE T1.num = T2.num)
    

    Or you could use a join to get the same result:

    SELECT T1.id, T1.num, T1.dis
    FROM table1 T1
    JOIN (
        SELECT num, MIN(dis) AS dis
        FROM table1
        GROUP BY num
    ) T2
    ON T1.num = T2.num AND T1.dis = T2.dis
    

    If you only want a single row from each group, even if there are ties then you can use this:

    SELECT id, dis, num FROM (
        SELECT id, dis, num, ROW_NUMBER() OVER (PARTITION BY num ORDER BY dis) rn
        FROM table1
    ) T1
    WHERE rn = 1
    

    Unfortunately this won’t be very efficient. If you need something more efficient then please see Quassnoi‘s page on selecting rows with a groupwise maximum for PostgreSQL. Here he suggests several ways to perform this query and explains the performance of each. The summary from the article is as follows:

    Unlike MySQL, PostgreSQL implements
    several clean and documented ways to
    select the records holding group-wise
    maximums, including window functions
    and DISTINCT ON.

    However to the lack of the loose index
    scan support by the PostgreSQL’s
    optimizer and the less efficient usage
    of indexes in PostgreSQL, the queries
    using these function take too long.

    To work around these problems and
    improve the queries against the low
    cardinality grouping conditions, a
    certain solution described in the
    article should be used.

    This solution uses recursive CTE’s to
    emulate loose index scan and is very
    efficient if the grouping columns have
    low cardinality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 395k
  • Answers 395k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer unzip -l Android.jar should do the trick. Another alternative: jar… May 15, 2026 at 2:34 am
  • Editorial Team
    Editorial Team added an answer Let's say your company makes 4 different products, your client… May 15, 2026 at 2:34 am
  • Editorial Team
    Editorial Team added an answer $('input:radio[name=rdName]').change(); May 15, 2026 at 2:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.