title: SQL Create column with group_id in SELECT statement
body: <p>I'm trying to create a column with group_id during the SELECT statement.</p>
  
  <p>As group we will take every contact_id and product_code combination till we find event = purchase. Then if there are other event with the same contact_id and product_code combination it will be assigned as another group. Also contact_id and product_code combination with no event = purchase will be an independent group.</p>
  
  <p>There are more than one contact_id-product_code combination and the data is not sorted in table.</p>
  
  <p>Below the table and the expected result after SELECT</p>
  
  <pre><code>contact_id | product_code | timestamp               | event      |
  ------------------------------------------------------------------
  contact_1  | product_1    | 2018-11-29 11:11:00.000 |   view     |
  contact_1  | product_1    | 2018-11-29 13:10:00.000 |   add      |
  contact_1  | product_1    | 2018-11-30 10:20:00.000 |   purchase |
  contact_1  | product_1    | 2018-12-03 10:20:00.000 |   mail     |
  contact_1  | product_1    | 2018-12-03 16:00:00.000 |   purchase |
  contact_2  | product_2    | 2018-12-05 19:01:00.000 |   add      |
  contact_2  | product_2    | 2018-12-05 19:03:00.000 |   purchase |
  contact_3  | product_3    | 2018-12-05 19:03:00.000 |   view     |
  contact_4  | product_4    | 2018-11-15 19:03:00.000 |   mail     |
  contact_4  | product_4    | 2018-11-15 19:03:00.000 |   purchase |
  contact_5  | product_5    | 2018-11-20 19:03:00.000 |   purchase |
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>contact_id | product_code | timestamp               | event      | id_groups|
  -----------------------------------------------------------------------------
  contact_1  | product_1    | 2018-11-29 11:11:00.000 |   view     |    1     |
  contact_1  | product_1    | 2018-11-29 13:10:00.000 |   add      |    1     |
  contact_1  | product_1    | 2018-11-30 10:20:00.000 |   purchase |    1     |
  contact_1  | product_1    | 2018-12-03 10:20:00.000 |   mail     |    2     |
  contact_1  | product_1    | 2018-12-03 16:00:00.000 |   purchase |    2     |
  contact_2  | product_2    | 2018-12-05 19:01:00.000 |   add      |    3     |
  contact_2  | product_2    | 2018-12-05 19:03:00.000 |   purchase |    3     |
  contact_3  | product_3    | 2018-12-05 19:03:00.000 |   view     |    4     |
  contact_4  | product_4    | 2018-11-15 19:03:00.000 |   mail     |    5     |
  contact_4  | product_4    | 2018-11-15 19:03:00.000 |   purchase |    5     |
  contact_5  | product_5    | 2018-11-20 19:03:00.000 |   purchase |    6     |
  </code></pre>