title: MySQL - getting sum() based on week/month numbers
body: <p>I have an expenses table called "transactions" that includes user_id, date_time and amount.</p>
  
  <p>What I am trying to do is to query for sum(amount) grouped by user_id based on week number / month, eg:</p>
  
  <p>1) top 10 spending users for week, say, 22 or 10</p>
  
  <p>2) top 10 spending users for, say, 3rd month</p>
  
  <p>I did look through the site and while there are similar SQL examples, they're mostly based on 7 day sliding windows or "last week" or "last month". I am still trying to see if I can figure it out using the MySQL docs but would appreciate any pointers that can save some time. </p>
  
  <p>Thank you!</p>
  
  <p>PS I am using Rails 3, maybe the framework has some built-in methods for this? The basic finders won't do...</p>
  
  <p>Later edit:</p>
  
  <p>I would have expected this to work, but it includes data from this week as well:</p>
  
  <pre><code>SELECT user_id,  sum(amount) AS t
   FROM transactions
   WHERE YEARWEEK(date_time) = YEARWEEK(now() - interval 1 week) 
  group by user_id
  </code></pre>