title: How to generate with SQL activity ranges given dates of events?
body: <p>I have a table in the following format:</p>
  
  <pre><code>| user_name | date       | number_of_visits |
  | cat005    | 2015-06-03 |      5           |
  | cat005    | 2015-06-08 |      1           |
  | dog009    | 2015-06-01 |      7           |
  | dog009    | 2015-06-19 |      2           |
  </code></pre>
  
  <p>So, for each user I have number of visits for a given date. If there were not visit at a given date for a given user, then there is no records in the data bases (in other words we do not <code>save number_of_visits</code> if it is equal to zero). </p>
  
  <p>Now I would like to use this table to generate another one in which for each user we have activity ranges. Here we used the following definition of being active: User is considered as "active" at a given day if he made at least one visit within the last 10 days. So, I would like to have something like that:</p>
  
  <pre><code>| user_name | active_start | active_end |
  | cat005    | 2015-03-02   | 2015-03-25 |    
  | cat005    | 2015-03-29   | 2015-06-01 |
  | dog009    | 2015-04-01   | 2015-06-01 |
  </code></pre>
  
  <p>Please note that the data in the two examples are not consistent. According to the used definition <code>active_end</code> is exclusive (which means that the user did not have a visit at this date). For example the first line in the above table means that the user was active at <code>2015-03-02</code> (he/she made at least one visit). One can also say that at <code>2015-03-01</code> (one day before) this user was not active, which, in its turn means that he/she made no visits for more that 10 days. One can also say that at <code>2015-03-25</code> use made no visit and it was day 11th day without visit (so, the user was "switched" by the system to an inactive state).</p>
  
  <p>How can one generated the second table using SQL.</p>