Group items with a date difference of 10 minutes between each other

5

I have a table named tb_log , in it I have some data id , usuario_id , produto_id , ..., data .

I need to group records by date as follows: All records that have a time difference of up to 10 minutes.

That is, if the first and second have a difference of 10 minutes, group, if the third has a 10-minute difference with the first or second, it enters the same grouping.

    
asked by anonymous 20.09.2016 / 21:51

1 answer

0

Here I am considering that your data field is TIMESTAMP :

SELECT
    data, 
    usuario_id,
    count(usuario_id)
FROM tb_log 
WHERE …
GROUP BY 
UNIX_TIMESTAMP(data) DIV 600, usuario_id
    
22.02.2017 / 00:26