title: SQL SERVER - Group records by n minutes interval
body: <p>I have a table like this:</p>
  
  <pre><code>     [SDate - DateTime]              [Value - Decimal]
      2010-11-16 10:32:22                     1
      2010-11-16 10:58:08                     2
  </code></pre>
  
  <p>and I need a query to group records by n minutes interval.</p>
  
  <p>Output can be for example(grouped by 60 minutes):</p>
  
  <pre><code>    2010-11-16 10:10:00   avg(Value)
      2010-11-16 10:11:00   avg(Value)
      2010-11-16 10:12:00   avg(Value)
  </code></pre>
  
  <p>Up to now, I have been using this query to group by 10 minutes interval because intervals were between 0 and 60 minutes:</p>
  
  <pre><code>...
  GROUP BY
  DATEPART(YEAR, SDate),
  DATEPART(MONTH, SDate),
  DATEPART(DAY, SDate),
  DATEPART(HOUR, SDate),
  (DATEPART(MINUTE, SDate) / 10)
  </code></pre>
  
  <p>But now the interval can be <strong>125436758</strong> minutes, for example.</p>
  
  <p>I can not create new tables and the user-defined interval must be n minutes.
  I am using SQL SERVER 2012. Thanks.</p>