title: How to correct the Partition over certain parameter Order by date that has identical dates in this query?
body: <p>I have this query below which assigns a new column called 'session_change' as 1.
  So this occurs whenever the difference between (date in current row) and (date from previous row) is greater than 30 minutes (30*60 seconds).</p>
  
  <p>These are the date / epoch / session_change:</p>
  
  <p><img src="https://i.stack.imgur.com/RiFIY.png" alt="Sample Output"></p>
  
  <p>But I have a problem.</p>
  
  <p>When there are identical dates in epoch seconds in consequent rows. It marks the last one among those identical epoch seconds as session_change if difference is greater than 30 minutes. (Identical epoch secs are records with these values 1475053048.00000)</p>
  
  <p>When it changes from 1474460665.00000 to 1475053048.00000, it marks the last record as you see in the snapshot.</p>
  
  <p>How to mark a session_change on the first record among those identical epoch seconds.</p>
  
  <p>Thanks very much for your help.</p>
  
  <pre><code>SELECT *
      , CASE 
            WHEN EXTRACT(EPOCH FROM date) - LAG(EXTRACT(EPOCH FROM date)) OVER (PARTITION BY userid ORDER BY date) &gt;= 30 * 60 
            THEN 1
       ELSE 0
     END as session_change
  FROM
    &lt;table_name&gt;
  order by date;
  </code></pre>