Reshaping — pivot & unpivot
Exercise · SQL & Querying

Predict, then run it

Predict the output

fleet(canoe, hull_hours, patch_count) = (c1,20,3),(c2,10,8). Per canoe, the metric furthest above its fleet average — via the unpivot → compute → re-pivot roundtrip:

WITH long AS (SELECT canoe,'hull_hours' AS metric, hull_hours AS v FROM fleet UNION ALL SELECT canoe,'patch_count',patch_count FROM fleet), dev AS (SELECT canoe, metric, v - AVG(v) OVER (PARTITION BY metric) AS above FROM long) SELECT canoe, metric FROM dev QUALIFY ROW_NUMBER() OVER (PARTITION BY canoe ORDER BY above DESC) = 1 ORDER BY canoe;

Predict the exact output.