# Getting the top results only by group in a .find statement

**URL:** <https://www.ruby-forum.com/t/getting-the-top-results-only-by-group-in-a-find-statement/165110>\
**Category:** Rails\
**Created:** [April 9, 2009, 8:05am UTC](https://www.ruby-forum.com/t/getting-the-top-results-only-by-group-in-a-find-statement/165110 "2009-04-09T08:05:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 9, 2009, 8:05am UTC](https://www.ruby-forum.com/t/getting-the-top-results-only-by-group-in-a-find-statement/165110/1 "2009-04-09T08:05:02Z")

</div>

This is my table

model name is bids

id | bid | keyword  
1 | 2 | orange  
2 | 3 | orange  
3 | 1 | red  
4 | 3 | blue  
5 | 6 | red

what i want is the get these the top bidder of each unique keyword in  
one .find statement

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 9, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/getting-the-top-results-only-by-group-in-a-find-statement/165110/2 "2009-04-09T13:18:48Z")

</div>

Tyl Tan wrote:

> This is my table
> 
> model name is bids
> 
> id | bid | keyword  
> 1 | 2 | orange  
> 2 | 3 | orange  
> 3 | 1 | red  
> 4 | 3 | blue  
> 5 | 6 | red
> 
> what i want is the get these the top bidder of each unique keyword in  
> one .find statement

As per my knowledge Table name should be in plurals and Model name  
should be singular.  
So assuming the Model name to be Bid, you can even do,

Bid.maximum(:bid, :group=\>“keyword”)

This will return the maximum bids of each unique keyword. The return  
type will be ActiveSupport::OrderedHash

Regards,  
Madhusudhan

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 11, 2009, 2:05am UTC](https://www.ruby-forum.com/t/getting-the-top-results-only-by-group-in-a-find-statement/165110/3 "2009-04-11T02:05:53Z")

</div>

Unfortunaly, .maximum will only give an array not the model instance  
that I want.

---

<div class="post-metadata">

**Author:** ![tyanlion](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@tyanlion](https://www.ruby-forum.com/u/tyanlion)\
**Post date:** [April 9, 2009, 10:49am UTC](https://www.ruby-forum.com/t/getting-the-top-results-only-by-group-in-a-find-statement/165110/4 "2009-04-09T10:49:49Z")

</div>

On Thu, Apr 9, 2009 at 8:04 AM, tyliong [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 5 | 6 | red
> 
> what i want is the get these the top bidder of each unique keyword in  
> one .find statement
> 
> >

assuming the table name is bids

The direct SQL for this would be  
select bids.\* from bids inner join (select max(bid) as bid, keyword  
from bids group by keyword) bids1 on bids.bid = bids1.bid and  
bids.keyword = bids1.keyword

in ActiveRecord it can be called as:  
Model.find\_by\_sql()  
or  
Model.find(:all, :select =\> ‘bids.\*’, :join =\> ’ inner join (select  
max(bid) as bid, keyword from bids group by keyword) bids1 on bids.bid  
= bids1.bid and bids.keyword = bids1.keyword’)  
which will allow you to add additional constraints

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain
