How do you handle complex joins when extracting data from a highly normalized schema?
Write a PostgreSQL query using the provided normalized tables. Include qualifying encounters from January 1 through March 31, 2025, and exclude cancelled or records with an unspecified status.
Output
- One row per patient and facility combination, including patients without qualifying encounters.
- Return
patient_id, patient_name, facility_id, facility_name, encounter_count, latest_encounter_date, and total_charges.
- Use zero counts and charges for patients without qualifying encounters, label missing facility names as
Unmatched facility, and order by patient_id then facility ID, with unmatched rows last.