title: SQL - Comparing rows in table with lag function based on column values
body: <p>I am trying to create a column named <code>previous_month</code> which based on the groups of distinct type/id looks to see for the previous row if <code>month_in</code> is one month less than the current row and if so <code>previous_month</code> = True and else False.</p>
  
  <pre><code>type    id  month_in    previous_month
  a       1   2019-09-01  FALSE
  a       1   2019-10-01  TRUE
  a       1   2019-11-01  TRUE
  a       1   2020-02-01  FALSE
  a       2   2020-01-01  FALSE
  a       2   2020-02-01  TRUE
  </code></pre>
  
  <p>I have tried using the lag function </p>
  
  <pre><code>Select 
         type, 
         id,
         month_for,
         lag(True, 1, False) over (partition by type, id order by type, id, month_for) as previous_month
  
  from myTable
  </code></pre>
  
  <p>however, this doesn't account for when  <code>month_in</code> increases by more than one month, i.e. I get this table:</p>
  
  <pre><code>type    id  month_in    previous_month
  a       1   2019-09-01  FALSE
  a       1   2019-10-01  TRUE
  a       1   2019-11-01  TRUE
  a       1   2020-02-01  TRUE
  a       2   2020-01-01  FALSE
  a       2   2020-02-01  TRUE
  </code></pre>
  
  <p>Any suggestions on if this is possible with the lag function? or if not the most efficient way to achieve this? I am working in snowflake sql. </p>