Write a SQL query to select only the first record of a duplicated row based on a specific timestamp or sequence number.
Treat rows with the same entity_id, event_type, and event_value as duplicates. Select the earliest timestamp, then the lowest sequence number, and use record_id as the final tie-breaker. Retain unique rows and place records with null timestamps after non-null timestamps.
record_id, entity_id, event_type, event_value, event_timestamp, sequence_number, and payload.entity_id, event_type, event_value with null values last, then record_id.| Column | Type | Description |
|---|---|---|
| record_idPK | INT | Unique record identifier |
| entity_id | INT | Identifier of the entity associated with the event |
| event_type | VARCHAR(50) | Type of recorded event |
| event_value | VARCHAR(100) | Value used as part of the duplicate business key |
| event_timestamp | TIMESTAMPTZ | Timestamp associated with the event |
| sequence_number | INT | Sequence used to resolve timestamp ties |
| payload | VARCHAR(255) | Additional event content |