title: t sql increment date column value based on previous row date value
body: <p>I have columns of data  in table #Temp1 as follows: (there is an existing clustered Index on the table of Unit asc, dateTime asc)</p>
  
  <pre><code>rowID unit   dateTime          newUnit  dateDiff
    1    A    2018-06-01 04:10      1       0
    2    A    2018-06-01 04:12      0       2
    3    A    2018-06-01 04:15      0       3
    4    A    2018-06-01 04:15      0       0
    5    A    2018-06-01 04:16      0       1
    6    A    2018-06-01 04:17      0       1
    7    B    2018-06-22 23:12      1       -305
    8    B    2018-06-01 23:14      0       2
    9    B    2018-06-01 23:18      0       4
   10    B    2018-05-22 23:18      0       0
   11    B    2018-06-01 23:22      0       4
   12    B    2018-06-01 23:31      0       9
  </code></pre>
  
  <p>Ignoring the first row, when dateDiff is 0 I want to increase dateTime by 2 minutes on the previous row value. If the increased value of a new dateTime means it is now equal to or greater than the dateTime in the next row after that, it should increase the next datetime and keep doing so until the difference is greater than 0. The process should restart when there is a change of unit (newUnit = 1). dateDiff does not need to be updated with the new differences. </p>
  
  <p>Next I want to update a another table #Temp2, that currently holds the old dateTime values, with the new dateTime values from #Temp1, based on rowID.</p>
  
  <p>After some research on S.O. I figured something like the following might work, but I don't have the SQL knowledge to crack it!</p>
  
  <pre><code>  WHILE (SELECT COUNT(*) FROM #Temp1) &gt; 0
          BEGIN
          UPDATE TOP (1) #Temp2  
          SET [dateTime] = DATEADD(minute, 2, #Temp1.[dateTime])
          FROM #Temp1
          WHERE #Temp2.moveID = #Temp1.moveID + 1  
             .... plus other conditions to update [dateTime]
      END
  </code></pre>
  
  <p>Any help on cracking this one is greatly appreciated.</p>