Select max


#1

How does one do a SELECT MAX query in rails? I’ve searched and searched
and
tried lots of things with no luck.

I want to implement "SELECT MAX(column_name) from table_name

and have it return the maximum value from column_name. I just need to
get
that one value, not the whole object…

Thanks for any ideas…

Shelby


#2

Try find_by_sql()


#3

Oh, I have!!! I must not be understanding the syntax, because I have not
been able to make it work…

Shelby


#4

tickets = Ticket.find_by_sql(“SELECT MAX(id) AS maxid FROM tickets”)
=> [#<Ticket:0x22ac01c @attributes={“maxid”=>“1”}>]

t = tickets[0]
=> #<Ticket:0x22ac01c @attributes={“maxid”=>“1”}>

t.maxid
=> “1”


#5

How does one do a SELECT MAX query in rails? I’ve searched and
searched and tried lots of things with no luck.

Sky Y. just suggested the following on the thread “SELECT SUM(?)
Query”:

…have a look at Calculation plugin. Once installed, you can use
something like:

Order.calculate(:sum, :cost, :group => :country,
:having => ‘sum(cost) > 50’)

The document uses :group_by, but it should be :group

If the link doesn’t come through, Calculations is available at http://
techno-weenie.net/blog/main/266/introducing-calculations.

-Ben


#6

On 12/30/05, Shelby W. removed_email_address@domain.invalid wrote:

How does one do a SELECT MAX query in rails? I’ve searched and searched and
tried lots of things with no luck.

I want to implement "SELECT MAX(column_name) from table_name

and have it return the maximum value from column_name. I just need to get
that one value, not the whole object…

Thanks for any ideas…

max_val = Model.connection.select_value(“select max(column_name) from
table_name”)


#7

Order.calculate(:sum, :cost, :group => :country,
:having => ‘sum(cost) > 50’)

The document uses :group_by, but it should be :group

If the link doesn’t come through, Calculations is available at
http://techno-weenie.net/blog/main/266/introducing-calculations.

Sorry, the svn has moved to
http://techno-weenie.net/svn/projects/plugins/calculations/

More tests than before, same crappy non-existent docs however.


rick
http://techno-weenie.net


#8

On Thu, Dec 29, 2005 at 11:25:49PM -0600, Shelby W. wrote:

How does one do a SELECT MAX query in rails? I’ve searched and searched
and tried lots of things with no luck.

I want to implement "SELECT MAX(column_name) from table_name

and have it return the maximum value from column_name. I just need to get
that one value, not the whole object…

This is another option:
SomeModel.find(:first, :select => ‘max(column_name) as max’).max

marcel


#9

When I do Wilson’s suggestion:

max_val = Model.connection.select_value(“select max(column_name) from
table_name”)

max_val comes in as a string, even though the database column is an int.
So
I have to convert the value using to_i.

Sorry for what must be a basic question, but do all such sql queries
return
strings?

Shelby


#10

On 12/30/05, Shelby W. removed_email_address@domain.invalid wrote:

Sorry for what must be a basic question, but do all such sql queries return
strings?

Shelby

Yup. ActiveRecord Column objects handle the type casting. For
instance, if you have an age column that’s an INT, it type casts it
with #to_i. I’m not sure if using Model.find() will do that on
max(age) or not though.

However, my Calculations plugin handles that. It assumes the type of
the column you’re calculating on.


rick
http://techno-weenie.net