Skip to content
Advertisement

Find duplicate data in last 1 hour

I am looking for a SQL script to find the data which has more than 2 entries in last 1 hour. I have a table having user_id & event_time. I want a way to find out if the user_id has more than 1 entries in last 1 hour.

I have tried below till now:

  1. Create temp table to put all duplicate entries :
  1. Run self Joins to fetch records having time difference of 1 hour or less:

Once first script is ran it gives around 800+ rows for duplicate data. But after running the second script the data I get is in thousands. Can anyone help here?

Advertisement

Answer

cross apply can be used to get all related events for each event according to your criteria as follows:

or you can get a list of events without binding to a specific event:

db<>fiddle

to get the events only for last hour, you can add the appropriate filter to Where clause in CTE.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement