Duplicate Parcel Registrations

Duplicate Parcel Registrations

You are the data quality analyst for a last-mile delivery platform. Warehouses upload parcel registrations in batches, and retrying a file can create several rows for the same physical parcel. Before carrier compensation is calculated, the billing team needs a duplicate audit so one parcel is not charged more than once. A registration's business key is the pair carrier_code + tracking_number. Consider only rows where both key components are present: the value is not NULL and is not empty after trimming leading and trailing spaces. Find business keys that occur more than once, count all rows in each duplicate group, and identify the first and last registration times. Business-key comparison is exact and case-sensitive. Count every row in a matching group, even when all non-key fields are identical. Return columns: - carrier_code — carrier code, text; - tracking_number — tracking number, text; - duplicate_count — number of rows with this business key, integer; - first_registered_at — earliest registration time, timestamp with time zone; - last_registered_at — latest registration time, timestamp with time zone. Sort by duplicate_count descending, then by carrier_code and tracking_number ascending. No rounding is required. Rows with an incomplete business key must be excluded. Resolve count ties with the stated secondary sort fields.

Expected output sample

This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables. Your answer must use the same column names as this sample — alias them with AS if needed.

carrier_codetracking_numberduplicate_countfirst_registered_atlast_registered_at
FEDEXFX300232026-05-01T11:20:00+00:002026-05-01T11:45:00+00:00
DHLTRK100122026-05-01T09:00:00+00:002026-05-01T09:05:00+00:00
UPSUP200122026-05-01T10:10:00+00:002026-05-01T11:00:00+00:00
PostgreSQLv16

Your query result will appear here

Focus radio
Paused · SomaFM · Fluid