January 31, 2017 at 6:54 am
Hello,
I have been tasked with the assignment of searching for BOTs in our database. I have 1 SQL table that tracks clicks for each report opened based on USERID. So each USERID represents a report click. I need a query that shows me how many clicks happened based on USERID in 20 second intervals, and the clicks need to be greater than 10.
Example: If UserID = '999' clicked on a report 25 times in 10 seconds, it is most likely a BOT and needs to be in the query. Here is what I have so far, and as always, thank you all in advance!
SELECT COUNT(UID),datetimestamp, DATEPART(SECOND, datetimestamp)
FROM TABLE
WHERE (datetimestamp = '01-01-2016')
AND DATEPART(SECOND, datetimestamp) >1 AND DATEPART(SECOND, datetimestamp) <21
GROUP BY UID, datetimestamp
The are no problems, only solutions. --John Lennon
January 31, 2017 at 7:11 am
i think your group by needs to round the timestamp to your interval of 20 seconds(DATEPART(SECOND, datetimestamp) %20, i think?) or at least to the nearest minute, which is a lot easier.
SELECT COUNT(UID),
DATEADD(minute, DATEDIFF(minute,0,datetimestamp), 0) AS Roundeddatetimestamp
FROM SomeTable
WHERE (datetimestamp = '01-01-2016')
--AND DATEPART(SECOND, datetimestamp) > 1 AND DATEPART(SECOND, datetimestamp) <21
GROUP BY UID, DATEADD(minute, DATEDIFF(minute,0,datetimestamp), 0)
Lowell
January 31, 2017 at 7:16 am
SELECT
COUNT(UID)
, DATEDIFF(second,'20160101',datetimestamp)/20 AS Interval
, UID
FROM TABLE
WHERE datetimestamp >= '20160101'
AND datetimestamp < '20160102'
GROUP BY
UID
, DATEDIFF(second,'20160101',datetimestamp)/20
HAVING COUNT(UID) > 10
January 31, 2017 at 7:39 am
How about:SELECT COUNT(T.UID),
DATEADD(second, CASE WHEN DATEPART(second,T.datetimestamp) < 20 THEN 0
WHEN DATEPART(second,T.datetimestamp) >= 40 THEN 40
ELSE 20
END,DATEADD(minute, DATEDIFF(minute,0,T.datetimestamp),0)),
T.UID
FROM
T
WHERE CAST(T.datetimestamp AS date) = '20160101'
GROUP BY DATEADD(second, CASE WHEN DATEPART(second,T.datetimestamp) < 20 THEN 0
WHEN DATEPART(second,T.datetimestamp) >= 40 THEN 40
ELSE 20
END,DATEADD(minute, DATEDIFF(minute,0,T.datetimestamp),0)),
T.UID
HAVING COUNT(T.UID) >= 20;
I really dislike having to re-align my code every time...
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
January 31, 2017 at 7:54 am
Thom A - Tuesday, January 31, 2017 7:39 AMHow about:SELECT COUNT(T.UID),
DATEADD(second, CASE WHEN DATEPART(second,T.datetimestamp) < 20 THEN 0
WHEN DATEPART(second,T.datetimestamp) >= 40 THEN 40
ELSE 20
END,DATEADD(minute, DATEDIFF(minute,0,T.datetimestamp),0)),
T.UID
FROMT
WHERE CAST(T.datetimestamp AS date) = '20160101'
GROUP BY DATEADD(second, CASE WHEN DATEPART(second,T.datetimestamp) < 20 THEN 0
WHEN DATEPART(second,T.datetimestamp) >= 40 THEN 40
ELSE 20
END,DATEADD(minute, DATEDIFF(minute,0,T.datetimestamp),0)),
T.UID
HAVING COUNT(T.UID) >= 20;
I really dislike having to re-align my code every time...
Thank you very much, this worked like a charm!
The are no problems, only solutions. --John Lennon
January 31, 2017 at 1:31 pm
Thom A - Tuesday, January 31, 2017 7:39 AMHow about:SELECT COUNT(T.UID),
DATEADD(second, CASE WHEN DATEPART(second,T.datetimestamp) < 20 THEN 0
WHEN DATEPART(second,T.datetimestamp) >= 40 THEN 40
ELSE 20
END,DATEADD(minute, DATEDIFF(minute,0,T.datetimestamp),0)),
T.UID
FROMT
WHERE CAST(T.datetimestamp AS date) = '20160101'
GROUP BY DATEADD(second, CASE WHEN DATEPART(second,T.datetimestamp) < 20 THEN 0
WHEN DATEPART(second,T.datetimestamp) >= 40 THEN 40
ELSE 20
END,DATEADD(minute, DATEDIFF(minute,0,T.datetimestamp),0)),
T.UID
HAVING COUNT(T.UID) >= 20;
I really dislike having to re-align my code every time...
Thanks Ten! Works perfect.
The are no problems, only solutions. --John Lennon
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply