Grady Health System needs a patient-level summary of active patients who had multiple completed visits during calendar year 2025. Write a PostgreSQL query using the provided tables.
Requirements
- Include only patients whose status is
Active and whose visits were completed between January 1 and December 31, 2025.
- Return the patient identifier, MRN, full name, completed visit count, total charges, latest visit date, and department for the latest visit.
- Include only patients with at least two qualifying visits, and sort by total charges descending, then patient ID ascending.
Use a CTE with ROW_NUMBER() to identify each patient's latest qualifying visit. A patient whose latest visit has no department should still appear with a NULL department.