Query SQL returns records only after 2 days prior to today

Asked

Viewed 222 times

1

Guys, as I am new to SQL, I need a help. I need to bring only the records that have not been moving for more than two days.

The query below is bringing everyone without this condition. I even ran a test using Datediff, but I’m not sure if that’s what it was.

Bringing all the records this condition

Qtde    Data
5000    18-07-2017
5001    18-07-2017
5002    25-07-2017

However, it was only to bring those of the 18th, as it is more than 2 days before today’s date. Below is the query I tried to do.

SELECT DISTINCT
S.SolID AS [Qtde - Chamados em Pausa],
MAX(CONVERT(DATE,T.TraData,103))
FROM Solicitacao S
LEFT JOIN Usuario U ON U.UsuID = S.UsuIDResponsavel
LEFT JOIN Tramite T ON T.SolID = S.SolID
WHERE S.VencimentoPausado = 1 AND
      T.TraData < DATEDIFF ( DAY ,2, getdate())
group by s.SolID 

1 answer

1


try the following code, see if it works because I did it blindly since it does not put the structure of the tables and I have no way to test here:

SELECT
S.SolID,
T.TraData
FROM Solicitacao S
INNER JOIN Tramite T on T.SolID = S.SolID and 
                    T.TraID = (SELECT TOP 1 
                                X.TraID 
                               FROM Tramite X 
                               WHERE X.SolID = S.SolID ORDER BY X.TraData DESC)
WHERE S.VencimentoPausado = 1 AND 
      cast(T.TraData as date) <= cast(DATEADD(day,-2,getdate()) as date)
  • Show Ball Guy. I do not put the structure of the tables because it is too big and some case is impossible to put here, but it worked. Thanks!

  • Good that helped, about the size, you can post the DDL, and always put the code between two acute accents ` or after four spaces for it to be formatted, size of the questions, I believe that is no problem. You can also use Sqlfiddle, because it helps a lot when analyzing the code and understanding the need.

Browser other questions tagged

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