Logistics & Transportation Analytics · Mission 12 of 30Medium

Detention hot spots — lanes with high dwell

HAVING clause to filter groups by an aggregate condition

The Brief

Dario RuizNetwork Planning Managerlogistics-ops

Detention charges are killing our budget. I need lanes where the average `dwell_hours_at_origin` exceeds 2.0 hours. Query `fact_shipments`, group by `lane_id`, compute avg dwell as `avg_dwell_hrs` (round to 2 decimals), count shipments as `shipment_count`. Use HAVING to filter. Sort by avg_dwell_hrs descending.

You'll practice

HAVINGGROUP BYORDER BY

Tables & columns available

fact_shipmentsfact15 columns
ColumnTypeKey
shipment_idINTPK
carrier_idINTFK → dim_carriers
lane_idINTFK → dim_lanes
planned_pickup_dateTEXT
actual_pickup_dateTEXT
planned_delivery_dateTEXT
actual_delivery_dateTEXT
qty_orderedINT
qty_shippedINT
weight_lbsREAL
freight_costREAL
fuel_surchargeREAL
accessorial_costREAL
dwell_hours_at_originREAL
statusTEXT

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

HAVING filters on aggregate results — unlike WHERE which filters individual rows, HAVING filters groups after GROUP BY.

Hint 2

Filter the grouped result, not the rows — there's a clause that runs after GROUP BY and accepts aggregate expressions.

Hint 3

GROUP BY lane_id and sort by avg_dwell_hrs DESC.