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 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Try find_by_sql() On 12/29/05, Shelby Westman <shelby.westman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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... > > Shelby > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Oh, I have!!! I must not be understanding the syntax, because I have not been able to make it work... Shelby On 12/29/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Try find_by_sql() > > > On 12/29/05, Shelby Westman <shelby.westman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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... > > > > Shelby > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
>> 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" On 12/29/05, Shelby Westman <shelby.westman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oh, I have!!! I must not be understanding the syntax, because I have not > been able to make it work... > > Shelby > > > On 12/29/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Try find_by_sql() > > > > > > On 12/29/05, Shelby Westman <shelby.westman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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... > > > > > > Shelby > > > > > > _______________________________________________ > > > Rails mailing list > > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
> How does one do a SELECT MAX query in rails? I''ve searched and > searched and tried lots of things with no luck.Sky Yin 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 :groupIf the link doesn''t come through, Calculations is available at http:// techno-weenie.net/blog/main/266/introducing-calculations. -Ben _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 12/30/05, Shelby Westman <shelby.westman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 Westman 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 -- Marcel Molina Jr. <marcel-WRrfy3IlpWYdnm+yROfE0A@public.gmane.org>
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20051230/71d7aa2d/attachment.html
On 12/30/05, Shelby Westman <shelby.westman@gmail.com> wrote:> 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? > > ShelbyYup. 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