title: Accessing previous row in SQL
body: <p>I have following data :</p>
  
  <pre><code>ID Begin_Dt End_DT 
  101 201205   201208
  101 201301   201309
  101 201401   201502
  101 201701   201801
  </code></pre>
  
  <p>Now if begin_DT is &lt;= 9 months from the previous instance end date than I need to override the end_dt or previous row wiht end_dt of next row. I need to repeat it untill difference is &lt;= 9</p>
  
  <p>Lets calculate difference >></p>
  
  <pre><code>Row_num ID Begin_Dt End_DT    Diff
  1   101 201205   201208    NA
  2   101 201301   201309    5
  3   101 201401   201502    4
  4   101 201701   201801    23
  </code></pre>
  
  <p>difference in row 2 and row 3 is &lt;=9 hence solution should be </p>
  
  <pre><code>ID Begin_Dt End_DT   Flag_corr
  101 201205   201502   1
  101 201301   201502   1
  101 201401   201502   0
  101 201701   201801   0
  </code></pre>