title: Getting aggregated values
body: <p>I have the below table - </p>
  
  <pre><code>UserId    Some_Value1   Datetime
  1            0            24-11-2016 13:00
  1            0            24-11-2016 13:45
  1            1            24-11-2016 15:00
  1            1            24-11-2016 17:15
  2            0            25-11-2016 11:23
  2            1            25-11-2016 13:22
  2            0            25-11-2016 14:01
  2            1            25-11-2016 18:00
  </code></pre>
  
  <p>As you can see - The value in <code>Some_Value1</code> is <code>1</code> when the datetime value with the previous row is greater than <code>1 Hour</code> for the same <code>userId</code></p>
  
  <p>I'm trying to get a series number rather than <code>1</code> and <code>0</code> when the <code>datetime</code> field is greater than <code>1 Hour</code>.</p>
  
  <p>Something like the below result - </p>
  
  <pre><code>UserId    Some_Value1             Datetime            Some_Value2
      1            0            24-11-2016 13:00            1
      1            0            24-11-2016 13:45            1
      1            1            24-11-2016 15:00            2
      1            1            24-11-2016 17:15            3
      2            0            25-11-2016 11:23            1
      2            1            25-11-2016 13:22            2
      2            0            25-11-2016 14:01            2
      2            1            25-11-2016 18:00            3
  </code></pre>
  
  <p>I'm trying to achieve this using <code>postgres</code> or <code>Redshift</code>. Tagging <code>Oracle</code> and <code>Mysql</code> to reach a larger audience and get pseudo-code as <code>SQL</code> query would mostly be the same for all databases except for inbuilt functions.</p>
  
  <p>All of the above data is for representational purpose only. This is just sample data and the real scenario would have more random data. Hence the code needs to be dynamic and not hard-coded.</p>