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

  • Home
  • SEARCH
  • 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 7663343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:02:24+00:00 2026-05-31T14:02:24+00:00

Not sure how to do this and not sure how to phrase it to

  • 0

Not sure how to do this and not sure how to phrase it to find the answer.

Say I have SELECT id, name, title, stuff FROM table

Id, name, and title can be whatever, it doesn’t matter. But I only want to select 1 column where stuff is distinct. So..

1, Bob, Builder, Hammer
2, Tom, Train, Track
3, Charlotte, Spider, Web
1, Bob, Builder, Children
2, Tom, Train, Children
3, Charlotte, Spider, Children

So I want to select the first three and only one of the next three.

  • 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-31T14:02:25+00:00Added an answer on May 31, 2026 at 2:02 pm

    you can try this:

    select
      max(id) as id,
      max(name) as name,
      max(title) as title,
      stuff
    from
      table
    group by
      stuff;
    

    we have the aggregation here (group by) and all the fields that is not enumerated in GROUP BY claus should be processed by some aggregate function. MAX is one of these functions. this mean the following. When sql engine finds several rows with the same value of stuff field, it shohuld collapse them to single row. But the other fields has different values. What one of three values should it take, saying, for field Name? We should point sql engine what to do in this case. By specifying aggregate function MAX for instance, we enforce sql engine to select the max of three values. In our case “Tom” will be returned for field Name. We could use some other aggregate function, e.g. MIN. It doesn’t matter in this case.

    Edition #2 (more safe):

    select
      t.id,
      t.name,
      t.title,
      t.stuff
    from
      (select
        max(id) as id,
        stuff
       from 
         table
       group by
        stuff
      ) as gt
    join
      table t on t.id = gt.id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not exactly sure how to phrase this, but here goes... We have a
Not sure how to phrase this question, but here is the problem. I have
I'm not quite sure how to phrase this, so bear with me. I have
I'm not quite sure how to phrase this question, but I couldn't find any
I'm not sure exactly how to phrase this, but here goes: I have a
Not really sure how to phrase this other than by example.. Given... DATABASES =
I'm not even sure how I should phrase this question. I'm passing some CustomStruct
I'm not sure this is possible, but in ruby, you can dynamically call a
I'm not quite sure on the best way to phrase this particular query, so
Not really sure how to phrase this question so it's generic enough. (and will

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.