SQL Query return records only after 2 days prior to today

1

Well, as a beginner in SQL, I need some help. I need only bring records that have not been moved for more than two days.

The query below is bringing everyone without this condition. I even did a test using the datediff, but I'm not sure if that was the case.

Bringing all records to this condition

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

However, it was only to bring those on the 18th, because it was more than 2 days ago compared to 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 
    
asked by anonymous 26.07.2017 / 02:31

1 answer

1

Try the following code, see if it works because I blindly did not put the structure of the tables and I can not 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)
    
26.07.2017 / 04:50