SQL - How to bring only rows (ID and Date/time) with less than 1 hour

Asked

Viewed 49 times

0

I need the following help:

I have a table that I need to compare the transactions with the same ID, but only bring those ID lines that have the previous transaction with the same ID with less than 1h.

Ex:

inserir a descrição da imagem aqui

Ai brings the lines with less than 1 hour between them:

inserir a descrição da imagem aqui

  • You have not informed which DBMS you are using, so make sure it provides "window functions" that will meet your needs.

  • 1

    I am using SQL Server

  • Which version of SQL Server are you using? Rows are returned in descending order of date/time and the first of the ID should always be returned?

  • 2013, yes and yes.

1 answer

0


Here is a suggested test using the Lag function to get a column value from a previous row:

select t.* 
from Tabela as t
where 
    datediff
        (minute, 
         lag(DataHora, 1, t.DataHora) 
             over(partition by id_omega order by DataHora desc), 
         t.DataHOra) < 60

I hope it helps

  • To make it 100% right I put what would be Where in select and took the t. of everything. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.