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

后端 未结 8 620
情歌与酒
情歌与酒 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:57

    Make a left join against the records with the same ip and a close time, and filter out the records where there is a match:

    select count(*) as visits
    from (
      select t.ip_address
      from tracking t
      left join tracking t2
        on t2.ip_address = t.ip_address
        and t2.timestamp > t.timestamp and t2.timestamp <= t.timestamp + 10
      where t2.ip_address is null
    ) x
    

提交回复
热议问题