Cameron Caine wrote in post #965790: > Marnen Laibow-Koser wrote in post #965782: >> Please avoid top-posting. >> >> Cameron Caine wrote in post #965778: >>> Thanks this certainly helps. Any reason in particular why you don't >>> recommend getting aggregates in the same query? >> >> I thought about that again after I posted it. It's less easy to use >> ActiveRecord's abstraction layer, and you may be getting a lot of data >> you don't need if you pack everything into the one query. But it's >> probably not as inadvisable as I had originally implied. >> > > Do you know of any resource I could read up on returning aggregates > along with the existing rows? I've never seen that before is all.
Any good SQL reference will show you how to do this. It will probably be somewhat less efficient if you have a lot of rows, because it has to put the aggregate value in each returned record, whereas if you did it in a separate query, it wouldn't have to return all that redundant data. > >>> >>> Things I need so far as is results like: >>> >>> Win Count >>> Loss Count >>> Draw Count >>> Win % >>> Home Wins % >>> Away Wins % >>> Scoring Average >> >> Yes? And how are these calculated? > > So far the win/loss is calculated from the field of 'home_score' and > 'away_score' > I could easily add an outcome integer column (0,1,2) to get an index on > it. That's a performance hack, and you probably don't need it: it should be very fast to have the DB calculate the difference of the two values on the fly. > > so it could be like > > class Game < AR::Base > scope :win, where(:outcome => 0) > @team.games.win > end > > Not sure if I could chain a sum/average onto that. Will have to look > into it. Chain it on? What do you mean? Best, -- Marnen Laibow-Koser http://www.marnen.org [email protected] -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

