Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this query (but don't try it on production data, but rather on a copy of this data):</p> <pre><code>update tabb_after_update tabb, ( select t.*, ( SELECT min( visit_id ) FROM tabb_after_update t1 WHERE t1.user_id = t.user_id AND t1.time_of_visit &lt;= t.time_of_visit AND t1.time_of_visit &gt;= subtime( t.time_of_visit, '00:20' ) ) new_id from tabb_after_update t ) tabb1 SET tabb.visit_id = tabb1.new_id WHERE tabb.user_id = tabb1.user_id AND tabb.visit_id = tabb1.visit_id ; </code></pre> <p>SQLFiddle demo --> <a href="http://www.sqlfiddle.com/#!2/caa08/1" rel="nofollow">http://www.sqlfiddle.com/#!2/caa08/1</a></p> <p><br><br><br> ------ EDIT ----- <br><br> Another version that "joins gaps" into one group if gaps are &lt;= 20 minutes.<br> </p> <pre><code>set @last_uid = 0; set @last_tm = '00:00'; set @last_vid = 0; update tabb_after_update tabb, ( select t.* , case when @last_uid = user_id AND cast( @last_tm as time) &gt;= subtime( time_of_visit, '00:20' ) then if( (@last_tm := time_of_visit ), @last_vid, @last_vid ) else if( (@last_uid := user_id) + (@last_vid := visit_id ) + (@last_tm := time_of_visit ), @last_vid, @last_vid ) end new_id from tabb_after_update t order by user_id, time_of_visit ) tabb1 SET tabb.visit_id = tabb1.new_id WHERE tabb.user_id = tabb1.user_id AND tabb.visit_id = tabb1.visit_id ; </code></pre> <p>SQLFiddle demo --> <a href="http://www.sqlfiddle.com/#!9/39f03/1" rel="nofollow">http://www.sqlfiddle.com/#!9/39f03/1</a> <br>In this demo the user 1 has entries from 17:10 to 17:50 with "gaps" betwen records less than 20 minutes, and the query "combines" all these records into one group.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload