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 296757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:34:41+00:00 2026-05-12T06:34:41+00:00

So I know this is a pretty dumb question, however (as the rather lengthily

  • 0

So I know this is a pretty dumb question, however (as the rather lengthily title says) I would like to know how do the following:

I have a table like this:

ID Foo Bar Blagh
----------------
1  10  20  30
2  10  5   1
3  20  50  40
4  20  75  12

I want to group by Foo, then pull out rows with minimum Bar, i.e. I want the following:

ID Foo Bar Blagh
----------------
2  10  5   1
3  20  50  40

I can’t for the life of me work out the correct SQL to retrieve this. I want something like:

SELECT ID, Foo, Bar, Blagh
FROM Table
GROUP BY Foo
HAVING(MIN(Bar))

However this clearly doesn’t work as that is completely invalid HAVING syntax and ID, Foo, Bar and Blagh are not aggregated.

What am I doing wrong?

  • 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-12T06:34:41+00:00Added an answer on May 12, 2026 at 6:34 am

    This is almost exactly the same question, but it has some answers!

    Here’s me mocking up your table:

    declare @Borg table (
        ID int,
        Foo int,
        Bar int,
        Blagh int
    )
    insert into @Borg values (1,10,20,30)
    insert into @Borg values (2,10,5,1)
    insert into @Borg values (3,20,50,70)
    insert into @Borg values (4,20,75,12)
    

    Then you can do an anonymous inner join to get the data you want.

    select B.* from @Borg B inner join 
    (
        select Foo,
            MIN(Bar) MinBar 
        from @Borg 
        group by Foo
    ) FO
    on FO.Foo = B.Foo and FO.MinBar = B.Bar
    

    EDIT Adam Robinson has helpfully pointed out that “this solution has the potential to return multiple rows when the minimum value of Bar is duplicated, and eliminates any value of foo where bar is null“

    Depending upon your usecase, duplicate values where Bar is duplicated might be valid – if you wanted to find all values in Borg where Bar was minimal, then having both results seems the way to go.

    If you need to capture NULLs in the field across which you are aggregating (by MIN in this case), then you could coalesce the NULL with an acceptably high (or low) value (this is a hack):

    ...
    MIN(coalesce(Bar,1000000)) MinBar -- A suitably high value if you want to exclude this row, make it suitably low to include
    ...
    

    Or you could go for a UNION and attach all such values to the bottom of your resultset.

    on FO.Foo = B.Foo and FO.MinBar = B.Bar
    union select * from @Borg where Bar is NULL
    

    The latter will not group values in @Borg with the same Foo value because it doesn’t know how to select between them.

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

Sidebar

Ask A Question

Stats

  • Questions 140k
  • Answers 140k
  • 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 ZedGraph is pretty good. May 12, 2026 at 7:59 am
  • Editorial Team
    Editorial Team added an answer Write decorator to allow access to certain model for a… May 12, 2026 at 7:59 am
  • Editorial Team
    Editorial Team added an answer as far as I know you cannot do this. the… May 12, 2026 at 7:59 am

Related Questions

This question is related to a previous post of mine Here . Basically, I
When I used to program embedded systems and early 8/16-bit PCs (6502, 68K, 8086)
I have been asked to look into FileMaker for creating a pretty simple database
I'm building a mobile application in VB.NET (compact framework), and I'm wondering what the
I know this is a simple question for someone out there, but I have

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.