title: SQL Query to add row number that resets everytime the column value changes (SQL Server 2014)
body: <p>Was looking through <code>Row_number()</code> function in SQL Server to increment the counter every time the value changes. The issue is that I need it to reset on a change such that the same value can reappear at a later stage and the <code>row_number</code> still resets and starts from 1 back again when you order it by datetime instead of grouping all the same type together and assigning a rownumber. </p>
  
  <p>For example:</p>
  
  <p><a href="https://i.stack.imgur.com/OXrFC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OXrFC.png" alt="TableA"></a></p>
  
  <p>when I do </p>
  
  <pre><code>select 
      *, 
      row_number() over (partition by type order by datetime) Order 
  from 
      TableA;
  </code></pre>
  
  <p>I get the below result (instead of the desired output shown above):</p>
  
  <p><a href="https://i.stack.imgur.com/ytoj9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ytoj9.png" alt="Output"></a></p>