Write a SQL query to identify and remove duplicate patient records while preserving the integrity of longitudinal study data.
The canonical record is the record with the lowest patient_id among patients sharing the same identifying details. Reassign all study visits from duplicate records before removing those records.
removed_patient_id and canonical_patient_id.removed_patient_id ascending.| Column | Type | Description |
|---|---|---|
| patient_idPK | INTEGER | Unique patient record identifier |
| first_name | VARCHAR(80) | Patient first name |
| last_name | VARCHAR(80) | Patient last name |
| date_of_birth | DATE | Patient date of birth |
| site_code | VARCHAR(20) | Clinical study site identifier |
| Column | Type | Description |
|---|---|---|
| visit_idPK | INTEGER | Unique study visit identifier |
| patient_id | INTEGER | Referenced patient record, nullable for an unmatched imported visit |
| visit_date | DATE | Date of the study visit |
| visit_type | VARCHAR(40) | Study visit classification |