title: SQL group by frequency within a date range
body: <p>I have a requirement to write a stored procedure that accepts a start date, end date and a frequency (day, week, month, quarter, year) and outputs a result set based on those parameters. Obviously, the simple part is the query by date range, but how do you group by frequency?</p>
  
  <p>So if have a set of raw data like this:</p>
  
  <pre><code>Date            Count
  ---------------------
  11/15/2011          6
  12/16/2011          9
  12/17/2011          2
  12/18/2011          1
  12/18/2011          4
  </code></pre>
  
  <p>And I call my stored proc like this:</p>
  
  <p>sp_Report '1/1/2011', '12/31/2011', 'week'</p>
  
  <p>I would expect results like this:</p>
  
  <pre><code>WeekOf          Count
  ---------------------
  11/19/2011          6
  12/17/2011         11
  12/24/2011          5
  </code></pre>
  
  <p>There are a couple of questions here:</p>
  
  <p>1) How do I determine the date for the end of the week (week ending on Sunday)?</p>
  
  <p>2) How do I group by that WeekOf date range?</p>