Stage times per package

reshaping

Stage times per package

Amazon SQL Interview Question

Amazon runs a fulfillment center on stage times: if packages sit waiting to be packed the answer is more packers, and if the pick walk is slow the answer is where the stock is kept. Every scan is written as its own event as a package moves through, and a package that has not reached a stage has no scan for it yet.

Return the package_id, and the time of each scan as received_at, picked_at, packed_at, shipped_at and delivered_at. A stage a package has not reached yet stays empty.

Asked of

  • Data Analyst
  • Analytics Engineer
  • Data Engineer
  • Data Scientist

amazon_fulfillment_scansTable24 rows

Column NameType
scan_idBIGINT
package_idVARCHAR
facilityVARCHAR
scanned_atTIMESTAMP
stageVARCHAR

amazon_fulfillment_scansExample Input

scan_idpackage_idfacilityscanned_atstage
101P001BLR12024-11-01 09:05:00received
102P001BLR12024-11-01 10:20:00picked
103P001BLR12024-11-01 11:15:00packed
104P001BLR12024-11-01 13:40:00shipped
105P001BLR12024-11-02 09:30:00delivered
123P006HYD32024-11-01 07:50:00received
124P006HYD32024-11-01 09:40:00picked

Example Output

package_idreceived_atpicked_atpacked_atshipped_atdelivered_at
P0012024-11-01 09:05:002024-11-01 10:20:002024-11-01 11:15:002024-11-01 13:40:002024-11-02 09:30:00
P0062024-11-01 07:50:002024-11-01 09:40:00NULLNULLNULL

Explanation

P001 reached every stage, so all five times are filled in. P006 has only been received and picked, so its three later columns are empty rather than the package dropping out of the report.

The example above is a small slice of the data. Your query runs against the full tables.