简体   繁体   中英

Select rows that have same username in table and compare date interval

I have a log table that contains

  • username
  • access_point
  • authdate

I would like to know which user have many associations on same access-point in 1 minute to find a connection problem.

I would like to have a query which test the multiple same username entry on the same access-point and proceed to compare one per one the first and second date interval Of course it is possible that there are more than 2 entry per same username.

Actually I can only return how many connection a user have in the same access-point.

More example:

If I have datas like this :

Username    Access_point          Authdate

ALAIN          AP01           2013-11-22 05:00:01
ALAIN          AP01           2013-11-22 04:50:01
ALAIN          AP02           2013-11-22 03:00:01

In this example all is OK because ALAIN was connected two time on AP01 but the connection interval on the same AP is 10 minutes.

But here:

 Username      Access_point    Authdate

 ALAIN          AP01           2013-11-22 05:00:01
 ALAIN          AP01           2013-11-22 04:59:10
 ALAIN          AP01           2013-11-22 04:59:01
 ALAIN          AP01           2013-11-22 04:58:50
 ALAIN          AP01           2013-11-22 04:57:55
 ALAIN          AP01           2013-11-22 04:50:01
 ALAIN          AP02           2013-11-22 03:00:01

ALAIN was connected multiple times on the same AP and interval is lower than a minute many times

you may try something like

SELECT U.username FROM your_table as U where

 EXISTS (
SELECT U2.username FROM your_table  as U2 WHERE 
 U.accesspoint = U2.accesspoint AND U.username = U2.username AND
DATEDIFF (U.authdate, U2.authdate) < what_you_want
)

[EDIT]

maybe the following

    SELECT DISTINCT U.username
    FROM table AS U
    WHERE (

    SELECT count( * )
    FROM table AS U2
    WHERE U.username = U2.username ... etc
    AND (
    (
    datediff(U.acces, U2.access)
    ) < your_limit
   )
    ) >1 #this to avoid same row to be returned twice

I think something like this will show you top records that you want

SELECT log_tmp.username, COUNT(log_tmp.username) as ctn 
FROM (SELECT username FROM log WHERE authdate > 'XXX' AND authdate < 'YYY') AS log_tmp
GROUP BY log_tmp.username ORDER BY cnt DESC

If you want to customize conditions, just do it in internal query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM