Forum: Ruby on Rails SELECT MAX

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shelby W. (Guest)
on 2005-12-30 07:27
(Received via mailing list)
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
Pat M. (Guest)
on 2005-12-30 07:36
(Received via mailing list)
Try find_by_sql()
Shelby W. (Guest)
on 2005-12-30 07:42
(Received via mailing list)
Oh, I have!!! I must not be understanding the syntax, because I have not
been able to make it work...

Shelby
Pat M. (Guest)
on 2005-12-30 07:54
(Received via mailing list)
>> 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"
Benjamin S. (Guest)
on 2005-12-30 08:27
(Received via mailing list)
> 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
Wilson B. (Guest)
on 2005-12-30 19:00
(Received via mailing list)
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")
Rick O. (Guest)
on 2005-12-30 19:22
(Received via mailing list)
> 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....

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
Marcel Molina Jr. (Guest)
on 2005-12-31 01:04
(Received via mailing list)
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
Shelby W. (Guest)
on 2005-12-31 01:46
(Received via mailing list)
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
Rick O. (Guest)
on 2005-12-31 01:52
(Received via mailing list)
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
This topic is locked and can not be replied to.