Write a SQL query to identify and remove duplicate records from a table without losing unique transaction history. Use the transactions table. Treat rows with the same non-null source_system and external_transaction_id as duplicates, retaining the earliest ingested_at record and using transaction_id as the tie-breaker.
duplicate_transaction_id, retained_transaction_id, source_system, and external_transaction_id.duplicate_transaction_id.| Column | Type | Description |
|---|---|---|
| transaction_idPK | BIGINT | Unique internal transaction identifier |
| source_system | VARCHAR(50) | System that supplied the transaction |
| external_transaction_id | VARCHAR(100) | Transaction identifier from the source system |
| customer_id | BIGINT | Customer associated with the transaction |
| transaction_date | DATE | Business date of the transaction |
| amount | NUMERIC(12,2) | Transaction amount |
| status | VARCHAR(20) | Transaction processing status |
| ingested_at | TIMESTAMP | Timestamp when the record entered the table |