title: Ruby Rails Complex SQL with aggregate function and DayOfWeek
body: <p><strong>Rails 2.3.4</strong></p>
  
  <p>I have searched google, and have not found an answer to my dilemma.</p>
  
  <p>For this discussion, I have two models. Users and Entries. Users can have many Entries (one for each day).</p>
  
  <p>Entries have values and sent_at dates.</p>
  
  <p>I want to query and display the average value of entries for a user BY DAY OF WEEK. So if a user has entered values for, say, the past 3 weeks, I want to show the average value for Sundays, Mondays, etc. In MySQL, it is simple:</p>
  
  <pre><code>SELECT DAYOFWEEK(sent_at) as day, AVG(value) as average FROM entries WHERE user_id = ? GROUP BY 1
  </code></pre>
  
  <p>That query will return between 0 and 7 records, depending upon how many days a user has had at least one entry.</p>
  
  <p>I've looked at find_by_sql, but while I am searching Entry, I don't want to return an Entry object; instead, I need an array of up to 7 days and averages...</p>
  
  <p>Also, I am concerned a bit about the performance of this, as we would like to load this to the user model when a user logs in, so that it can be displayed on their dashboard. Any advice/pointers are welcome. I am relatively new to Rails.</p>