Comparing Date Fields in SQL Server

I have a flat file that I cleaned up using SSIS, the result is as follows:

MEDICAL ADMIT PATIENT PATIENT DATE OF DX 
REC NO DATE NUMBER NAME DISCHARGE Code DRG #

123613 02/16/09 12413209 MORIBALDI, GEMMA 02/19/09 428.20 988    
130897 01/23/09 12407193 TINLEY, PATRICIA 01/23/09 535.10 392    
139367 02/27/09 36262509 THARPE, GLORIA 03/05/09 562.10 392 

141954 02/25/09 72779499 SHUMATE, VALERIA 02/25/09 112.84 370   
141954 03/07/09 36271732 SHUMATE, VALERIA 03/10/09 493.92 203  

145299 01/21/09 12406294 BAUGH, MARIA 01/21/09 366.17 117  

and a report (final results) attached to the screen, taken from the final Excel report.

so what happens if the same name or number of one account is duplicated, this means that the patient has again entered the hospital and should be included in the report. ! [alt text] [1]

What do I need to do...

Eliminate any rows that are not duplicated (not all were allowed in this file again) and compare dates to get ReAdmitdate and ReDischargedate

I dumped the data into an SQL table and tried to compare the dates to find out "ReAdmitdate" and "ReDischargedate"

any help is appreciated.

+3
source share
2 answers

? , patient_number , medical_rec_no - .

, , .

SELECT p1.patient_number, p1.admit_date,
  p2.admit_date as readmit_date, p2.discharge_date AS redischarge_date
FROM patient p1
  INNER JOIN patient p2
    ON p1.patient_number = p2.patient_number AND
      p2.medical_rec_no <> p1.medical_rec_no
  LEFT OUTER JOIN patient p3
    ON p3.patient_number = p1.patient_number AND
      p3.medical_rec_no <> p1.medical_rec_no AND
      p3.admit_date < p2.admit_date
WHERE p3.patient_number IS NULL
ORDER BY p1.patient_number, p1.admit_date, p1.medical_rec_no
+1

Data Warehousing ( , ), , , ( ). , SQL.

0

Source: https://habr.com/ru/post/1740159/


All Articles