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

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

    The following logic will only count a visit as a 'unique visit' if there wasn't a preceding record from the same ip address within the preceding 10 seconds.

    This means that {1,11,21,32,42,52,62,72} will count as 2 visits, with 3 and 5 tracks each, respectively.

    It accomplishes this by first identifying the unique visits. Then it counts all visits that happened between that unique visit and the next unique visit.

    WITH
        unique_visits
    (
      SELECT
        ip_address, time_stamp
      FROM
        visitors
      WHERE
        NOT EXISTS (SELECT * FROM visitors AS [previous]
                    WHERE ip_address  = visitors.ip_address
                      AND time_stamp >= visitors.timestamp - 10
                      AND time_stamp <  visitors.timestamp)
    )
    SELECT
      unique_visitors.ip_address,
      unique_visitors.time_stamp,
      COUNT(*) AS [total_tracks]
    FROM
      unique_visitors
    INNER JOIN
      visitors
        ON  visitors.ip_address  = unique_visitors.ip_address
        AND visitors.time_stamp >= unique_visitors.time_stamp
        AND visitors.time_stamp <  ISNULL(
                                      (SELECT MIN(time_stamp) FROM unique_visitors [next]
                                       WHERE  ip_address = unique_visitors.ip_address
                                       AND    time_stamp > unique_visitors.ip_address)
                                      , visitors.time_stamp + 1
                                   )
    

    You will also want either an index or primary key on (ip_address, time_stamp)

提交回复
热议问题