When I run the script it does not return only the maximum value, ie the longest time, but the 3 times.
SELECT P.CD_PACIENTE CD_PACIENTE
,P.NM_PACIENTE NM_PACIENTE
,L.CD_UNID_INT CD_UNID_INT
,L.DS_ENFERMARIA DS_ENFERMARIA
,L.DS_LEITO DS_LEITO
,MAX(MI.HR_MOV_INT) HR_MOV_INT
FROM ATENDIME A
JOIN PACIENTE P ON P.CD_PACIENTE = A.CD_PACIENTE
JOIN MOV_INT MI ON MI.CD_ATENDIMENTO = A.CD_ATENDIMENTO
JOIN LEITO L ON L.CD_LEITO = MI.CD_LEITO
WHERE A.DT_ALTA_MEDICA IS NULL
AND P.CD_PACIENTE = 0
GROUP BY P.CD_PACIENTE
,P.NM_PACIENTE
,L.CD_UNID_INT
,L.DS_ENFERMARIA
,L.DS_LEITO
ORDER BY P.CD_PACIENTE
Result returned:
0 TESTE 35 0319 VIRTUAL 11/06/2018 10:51:18
0 TESTE 23 0414 LEITO03 11/06/2018 10:49:48
0 TESTE 35 0319 LEITO008 01/04/2018 16:00:00
Expected result:
0 TESTE 35 0319 VIRTUAL 11/06/2018 10:51:18