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
Oh, I have!!! I must not be understanding the syntax, because I have not
been able to make it work…
Shelby
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
On 12/30/05, Shelby W. [email protected] 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”)
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
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
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
On 12/30/05, Shelby W. [email protected] 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