Relationship dates as a result of SQL query on Oracle

Asked

Viewed 102 times

2

Good afternoon,

When carrying out the following consultation SELECT CD_ATENDIMENTO,DT_ATENDIMENTO FROM ATENDIME I get the following return

Consulta

However, I need to return only the results that have less than 90 days of difference between one and the other, that would be the 4th and 5th record.

Thanks for the help!!

  • Alexandre, the DT_ATENDIMENTO field is of the date type?

2 answers

0

You can use the following:

select CD_ATENDIMENTO, DT_ATENDIMENTO
from ATENDIME
where DT_ATENDIMENTO >= trunc(sysdate - interval '60' DAY)

With this, you will bring the records with DT_ATENDIMENTO greater than or equal to 60 before today’s date (if another parameter, you can change the sysdate)

  • 1

    From what I understand, he wants the records that have less difference between one and the other and not necessarily between the current date or a specific date. But let’s wait for his demonstration.

  • Correct Filipe, I used the LAG command to try to make this comparison, but falls into another problem, not grouped by the employee. Explaining better, I need to filter all patients who have already been hospitalized and discharged, but have readmitted until 30 days after.

0

Follow the command I made and the result

SELECT ATENDIME.CD_ATENDIMENTO, ATENDIME.CD_PACIENTE, PACIENTE.NM_PACIENTE, ATENDIME.DT_ATENDIMENTO, ATENDIME.DT_ALTA,LAG(DT_ALTA,1)OVER (ORDER BY ATENDIME.CD_PACIENTE) AS ULT
FROM ATENDIME JOIN (SELECT CD_PACIENTE FROM ATENDIME WHERE CD_LEITO IN (SELECT CD_LEITO FROM LEITO WHERE CD_UNID_INT IN (10,11,18)) GROUP BY CD_PACIENTE HAVING COUNT(CD_PACIENTE)> 1) A ON A.CD_PACIENTE = ATENDIME.CD_PACIENTE
JOIN PACIENTE ON PACIENTE.CD_PACIENTE = ATENDIME.CD_PACIENTE
WHERE CD_LEITO IN (SELECT CD_LEITO FROM LEITO WHERE CD_UNID_INT IN (10,11,18)) ORDER BY ATENDIME.CD_PACIENTE

inserir a descrição da imagem aqui From then on I would compare the ULT field with DT_ATENDIMENTO, but it brings the data of other patients, and it is necessary to list only those who had the readmission until 30 days after their discharge

Browser other questions tagged

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