One register for every delivery leg

One register for every delivery leg

You are an analyst at a delivery company. Parcels move around the city with couriers and between cities with trucks, and historically these two flows are written into separate tables. Before the month is closed, finance asks for a single end-to-end register of the legs worked: who carried what parcel and over what distance. For them every row is a separate billable fact, so identical records must not be collapsed: if a courier ran the same leg for the same parcel twice, they get paid twice. Return columns: - leg_source — where the record comes from: the text 'courier' for city legs and 'truck' for intercity legs; - parcel_code — parcel code; - handler_name — the courier's name for city legs or the driver's name for intercity legs; - distance_km — leg distance in kilometres, exactly as stored, with no rounding. Sorting: by parcel_code, then leg_source, then handler_name, then distance_km, all ascending. Do not remove duplicates: fully identical rows must appear in the result as many times as they occur in the source tables.

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.

leg_sourceparcel_codehandler_namedistance_km
courierPCL-1001Ivan Petrov3.5
truckPCL-1001Sergey Lomov210
courierPCL-1002Olga Sokolova7.2
PostgreSQLv16

Your query result will appear here

Focus radio
Paused · SomaFM · Fluid