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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:58:46+00:00 2026-05-13T00:58:46+00:00

I have a table with (among other things) a name and a rank. I’d

  • 0

I have a table with (among other things) a name and a rank. I’d like to return the set of all unique names, but for each name returned, I’d like to pick the row with the highest rank. This is simple with two nested SELECT statements:

SELECT * FROM (SELECT * FROM foo ORDER BY rank DESC) AS ordered GROUP BY name

MySQL takes the first “hit” for each name, which (because of the earlier ORDER BY) will always be the highest-ranking one.

Now, if I want to wire into this table with ActiveRecord, I’m at a bit of a loss. I could just throw the above into find_by_sql, but that just feels dirty. I tried something like

result = foo.all
result.delete_if do |item|
  isOutranked = false
  result.each do |row|
    if (row.name == item.name) and (row.rank > item.rank) then isOutranked = true
  end
  isOutranked
end

I think that works, but it still feels like there ought to be a better way. Solving it either via ActiveRecord trickery or a more elegant array manipulation would be welcome!

  • 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-13T00:58:47+00:00Added an answer on May 13, 2026 at 12:58 am

    MySQL takes the first “hit” for each name, which (because of the earlier ORDER BY) will always be the highest-ranking one.

    The query you’re using to return the top row in the group is not guaranteed. It’s only a coincidence of the implementation, and it’s subject to change. Do not rely on this.

    You are trying to solve the “greatest-n-per-group” problem that I see posted on StackOverflow frequently. Here is a query that gets the answer more reliably:

    SELECT t1.*
    FROM foo AS t1
    LEFT OUTER JOIN foo AS t2
     ON (t1.name = t2.name AND t1.rank < t2.rank)
    WHERE t2.name IS NULL;
    

    Alternative that does the same thing:

    SELECT *
    FROM foo AS t1
    WHERE NOT EXISTS 
        (SELECT * FROM foo AS t2
         WHERE t1.name = t2.name AND t1.rank < t2.rank);
    

    I could just throw the above into find_by_sql, but that just feels dirty.

    ActiveRecord is very handy for certain kinds of queries, but you can’t solve every database query with ActiveRecord. The sooner you get over this notion the sooner you will get your work done and be successful. SQL is not going to bite you.

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

Sidebar

Related Questions

I have an HTML table whose cells contain, among other things, spans, like this:
I have a table that consists of, among other things, two fields named StartTime
I have a table with (among other things) dates in a field. I need
I have a database table that contains among other things partial postal codes. I'm
I have a table that contains, among other things, about 30 columns of boolean
Ok, I have this first table which has, among other things: table 1: id
We have a table containing coordinates, among other things. We used to store these
Here's my problem. Suppose I have a table called persons containing, among other things,
I have a web application which among other things contains a table of items
Simply put, I have a table with, among other things, a column for timestamps.

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.