Tell me about a time you used SQL to insert data, union results, and debug a query involving specific column names.
Write a PostgreSQL query that inserts completed inpatient and outpatient encounters into the unified table and returns the inserted rows. Ensure the target's required identifier is populated and use the exact source and target column names.
Output
- One row per newly inserted encounter.
- Columns:
activity_id, encounter_id, patient_id, activity_date, and source_system.
- Exclude non-completed source records and sort by
source_system, activity_date, and activity_id ascending.