title: SQL Server: Average counts by hour and day of week
body: <h3>Background</h3>
  <p>I have a table set up in a SQL Server environment that contains a log of various activity that I'm tracking.  Particular log items use unique codes to categorize what activity is taking place and a datetime field tracks when that activity occurred.</p>
  <h3>Problem</h3>
  <p>I would like to, using either a single query or a stored procedure, get an average of hourly counts of activity, grouped by day of the week.  Example:</p>
  <pre><code>Day      | Hour | Average Count
  -------------------------------
  Monday   | 8    | 5
  Monday   | 9    | 5
  Monday   | 10   | 9
  ...
  Tuesday  | 8    | 4
  Tuesday  | 9    | 3
  ...etc
  </code></pre>
  <p>Right now I've got a query setup that spits out the counts per hour per day, but my problem is taking it a step further and getting average by day of week.  Here's my current query:</p>
  <pre><code>SELECT CAST([time] AS date) AS ForDate,
     DATEPART(hour, [time]) AS OnHour,
     COUNT(*) AS Totals
  FROM [log] WHERE [code] = 'tib_imp.8'
  GROUP BY CAST(time AS date),
     DATEPART(hour,[time])
     ORDER BY ForDate Asc, OnHour Asc
  </code></pre>
  <p>Any suggestions as to how I might accomplish this?</p>
  <p>Thanks in advance!</p>