Count number of rows that are not within 10 seconds of each other

后端 未结 8 607
情歌与酒
情歌与酒 2021-02-01 09:26

I track web visitors. I store the IP address as well as the timestamp of the visit.

ip_address    time_stamp
180.2.79.3  1301654105
180.2.79.3  1301654106
180.2.         


        
8条回答
  •  独厮守ぢ
    2021-02-01 09:48

    As usual with SQL there are many solution for your problem. I would use following query which is simple and should be "good enough":

    SELECT COUNT(*) AS tracks 
    FROM (
        SELECT ip_address 
        FROM tracking 
        GROUP BY ip_address, FLOOR(time_stamp / 10)
    )
    

    The sub query groups visits of a single user in 10s intervals so that they are counted as one visit.

    Of cause it is possible to find cases in which two visits will appear in different 10s window even though the interval between this visits will be less than 10s. It would require much more complex logic to eliminate such cases and the analytical value of this added complexity would be dubious (10s interval sounds like an arbitrary value anyway).

提交回复
热议问题