Doubt - SQL Server 2012 Query

Asked

Viewed 61 times

1

People, in the query below want to put a condition within the Where that bring only the tasks that are due two months after the opening date. For example. The task 11111 was opened on 01-11-2017 and is due 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
  • The Task table already has the correct due date ?

  • 1

    yes, all tasks already exist a due date.

1 answer

4


A possible way, 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 date data, the maturity is 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;

Browser other questions tagged

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