Skip to content
Advertisement

How to filter records in sql server? [closed]

I have a table called “Status” as shown below in sql server.

enter image description here

How can I filter the records where the timestamp is less then 5 minutes for the same Level and same Status?

Eg: For the above records my output should be like

enter image description here

Advertisement

Answer

Use lag():

select s.*
from (select s.*, lag(date) over (partition by level, status order by date) as prev_date
      from status s
     ) s
where prev_date is null or
      prev_date > dateadd(minute, -5, date);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement