Doubt - SQL Server 2012 Query

1

Galera, in the query below to want to place a condition inside the where that only brings the tasks that they have with maturity two months after the opening date. For example. Task 11111 was opened on 11/01/2017 and has expiration on 01-01-2018, so it is to return this task in the query.

SELECT
  tarefaid,
  dataabertura,
  datavencimento
FROM Tarefa t
LEFT JOIN Usuario ur
  ON ur.UsuID = t.UsuIDResponsavel
LEFT JOIN Usuario uc
  ON uc.UsuID = t.UsuIDCliente
WHERE t.TarTipID IN (727, 1053)
AND t.TarNumAtiv = 0
AND t.TarStatus <> 9
AND t.TarEstagioID IN (240, 461)
AND ur.UsuIDGrupo = 30
AND uc.UsuTipo = 'C'
AND uc.EmpLiberada = 1
AND MONTH(t.tarvencimento) = 01
    
asked by anonymous 21.12.2017 / 15:22

1 answer

2

A possible form, if the comparison were the current date, could use the following:

SELECT
  tarefaid,
  dataabertura,
  datavencimento
FROM Tarefa t
LEFT JOIN Usuario ur
  ON ur.UsuID = t.UsuIDResponsavel
LEFT JOIN Usuario uc
  ON uc.UsuID = t.UsuIDCliente
WHERE t.TarTipID IN (727, 1053)
AND t.TarNumAtiv = 0
AND t.TarStatus <> 9
AND t.TarEstagioID IN (240, 461)
AND ur.UsuIDGrupo = 30
AND uc.UsuTipo = 'C'
AND uc.EmpLiberada = 1
AND DATEADD(MONTH, 2, t.dataabertura) >= GETDATE();

Using the maturity data, with maturity equal to or greater than 2 months:

SELECT
  tarefaid,
  dataabertura,
  datavencimento
FROM Tarefa t
LEFT JOIN Usuario ur
  ON ur.UsuID = t.UsuIDResponsavel
LEFT JOIN Usuario uc
  ON uc.UsuID = t.UsuIDCliente
WHERE t.TarTipID IN (727, 1053)
AND t.TarNumAtiv = 0
AND t.TarStatus <> 9
AND t.TarEstagioID IN (240, 461)
AND ur.UsuIDGrupo = 30
AND uc.UsuTipo = 'C'
AND uc.EmpLiberada = 1
AND DATEDIFF(MONTH, t.dataabertura, t.datavencimento) >= 2;
    
21.12.2017 / 15:46