2
I am trying to generate the query below as follows.
Cliente Tarefa Data da Ultima Providencia Teste 16555 04-07-2017
However, is generating duplicate, IE, I want to take the last date of the last provision, however, is bringing all dates even using the max
.
Cliente Tarefa Data da Ultima Providencia Teste 16555 04-07-2017 Teste 16555 02-06-2017
SELECT uc.usurazaosocial [cliente - razão social],
uc.usunome [cliente - nome fantasia],
t.tarid [tarefa],
ur.usunome [responsável - tarefa],
t.tartitulo [título],
MAX(CONVERT(date, a.ativdata, 103))[data última providência],
ISNULL(uta.usunome, utg.usunome) [responsável tramite],
dbo.dias_uteis(MAX(CONVERT(date, a.ativdata, 103)), GETDATE()) [dias sem movimentação],
t.tarnumativ [número da providência],
s.statusdesc [status]
FROM tarefa t
LEFT JOIN usuario uc ON uc.usuid = t.usuidcliente
LEFT JOIN usuario ur ON ur.usuid = t.usuidresponsavel
LEFT JOIN atividade a ON a.tarid = t.tarid
LEFT JOIN usuario uta ON uta.usuid = a.usuidalteracao
LEFT JOIN usuario utg ON utg.usuid = a.usuid
LEFT JOIN status s ON s.codstatus = t.tarstatus
WHERE t.projid IS NOT NULL
AND a.ativorigem = 'S'
AND t.tarid = 161574
GROUP BY uc.usurazaosocial,
uc.usunome,
t.tarid,
t.tartitulo,
t.tarnumativ,
s.statusdesc,
ur.usunome,
uta.usunome,
utg.usunome
ORDER BY uc.usurazaosocial DESC
Duplicate of where? Recommended [tour].
– UzumakiArtanis
But you are returning many more fields than the 3 you quote.
– Sorack
@Stormwind, the task is duplicated, it was to bring only the maximum date.
– Renan Bessa
@Sorack, I’ve put these three fields in the single demonstrative question. The other fields have the same value, what differs is only the date that in the case was to bring only the first record with the maximum date.
– Renan Bessa
Boot one
DISTINCT
shortly after theSELECT
and theMAX
on the date to test.– Sorack
had already done this test @Sorack, but it didn’t work
– Renan Bessa
What is the primary key of the table
atividade
?– Sorack
@Sorack, it’s the Ativid field
– Renan Bessa