title: SQL Server Group by Count of DateTime Per Hour?
body: <pre><code>    create table #Events
  (
      EventID int identity primary key,
      StartDate datetime not null,
      EndDate datetime not null
  )
  go
  insert into #Events (StartDate, EndDate)
  select '2007-01-01 12:44:12 AM', '2007-01-01 12:45:34 AM' union all
  select '2007-01-01 12:45:12 AM', '2007-01-01 12:46:34 AM' union all
  select '2007-01-01 12:46:12 AM', '2007-01-01 12:47:34 AM' union all
  select '2007-01-02 5:01:08 AM', '2007-01-02 5:05:37 AM' union all
  select '2007-01-02 5:50:08 AM', '2007-01-02 5:55:59 AM' union all
  select '2007-01-03 4:34:12 AM', '2007-01-03 4:55:18 AM' union all
  select '2007-01-07 3:12:23 AM', '2007-01-07 3:52:25 AM'
  </code></pre>
  
  <p>(with apologies to <a href="http://www.sqlteam.com/article/working-with-time-spans-and-durations-in-sql-server" rel="noreferrer">http://www.sqlteam.com/article/working-with-time-spans-and-durations-in-sql-server</a> for harvesting their base sql)</p>
  
  <p>I am trying to find the count of Events that occurred in an hour, so the result set would look like this:</p>
  
  <pre><code>2007-01-01      12:00     3
  2007-01-02       5:00     2
  2007-01-03       4:00     1
  2007-01-07       3:00     1
  </code></pre>
  
  <p>I have been playing with dateadd and round and grouping but not getting it. Can anyone help?</p>
  
  <p>Thanks.</p>