#!/home/rosy/bin/sql-run --show-script --color=always

-- GOAL: Compare all warehouses against the top performer
-- We'll build this step by step!

-- ============================================
-- STEP 1: Get total deliveries per warehouse
-- ============================================
-- This sums up all shipments for each warehouse in 2023

SELECT
    warehouse_id,
    warehouse_name,
    SUM(total_shipments) AS total_deliveries
FROM daily_delivery_metrics
WHERE YEAR(report_date) = 2023
GROUP BY warehouse_id, warehouse_name
ORDER BY total_deliveries DESC
LIMIT 5;

-- ============================================
-- STEP 2: Find just the TOP warehouse's number
-- ============================================
-- We need to isolate the highest total so we can compare against it

SELECT MAX(total_deliveries) AS top_deliveries
FROM (
    SELECT SUM(total_shipments) AS total_deliveries
    FROM daily_delivery_metrics
    WHERE YEAR(report_date) = 2023
    GROUP BY warehouse_id
) AS totals;

-- ============================================
-- STEP 3: Use CTEs to make this cleaner
-- ============================================
-- CTE = "Common Table Expression" = a temporary named result
-- Think of it like saving a query result to use later

WITH warehouse_totals AS (
    -- This CTE calculates totals per warehouse
    SELECT
        warehouse_id,
        warehouse_name,
        SUM(total_shipments) AS total_deliveries
    FROM daily_delivery_metrics
    WHERE YEAR(report_date) = 2023
    GROUP BY warehouse_id, warehouse_name
)
SELECT * FROM warehouse_totals ORDER BY total_deliveries DESC LIMIT 3;

-- ============================================
-- STEP 4: Add a second CTE for the top value
-- ============================================

WITH warehouse_totals AS (
    SELECT
        warehouse_id,
        warehouse_name,
        SUM(total_shipments) AS total_deliveries
    FROM daily_delivery_metrics
    WHERE YEAR(report_date) = 2023
    GROUP BY warehouse_id, warehouse_name
),
top_warehouse AS (
    -- This CTE grabs ONLY the top delivery count
    SELECT total_deliveries
    FROM warehouse_totals
    ORDER BY total_deliveries DESC
    LIMIT 1
)
SELECT * FROM top_warehouse;

-- ============================================
-- STEP 5: CROSS JOIN to attach top to every row
-- ============================================
-- CROSS JOIN = "attach this value to EVERY row"
-- Unlike regular JOINs, no matching condition needed

WITH warehouse_totals AS (
    SELECT
        warehouse_id,
        warehouse_name,
        SUM(total_shipments) AS total_deliveries
    FROM daily_delivery_metrics
    WHERE YEAR(report_date) = 2023
    GROUP BY warehouse_id, warehouse_name
),
top_warehouse AS (
    SELECT total_deliveries
    FROM warehouse_totals
    ORDER BY total_deliveries DESC
    LIMIT 1
)
SELECT
    w.warehouse_name,
    w.total_deliveries,
    t.total_deliveries AS top_deliveries
FROM warehouse_totals w
CROSS JOIN top_warehouse t
ORDER BY w.total_deliveries DESC
LIMIT 3;

-- ============================================
-- STEP 6: Calculate the percentage difference
-- ============================================
-- Formula: (mine - best) / best * 100
-- Results will be 0 for top, negative for others

WITH warehouse_totals AS (
    SELECT
        warehouse_id,
        warehouse_name,
        SUM(total_shipments) AS total_deliveries
    FROM daily_delivery_metrics
    WHERE YEAR(report_date) = 2023
    GROUP BY warehouse_id, warehouse_name
),
top_warehouse AS (
    SELECT total_deliveries
    FROM warehouse_totals
    ORDER BY total_deliveries DESC
    LIMIT 1
)
SELECT
    w.warehouse_name,
    w.total_deliveries,
    t.total_deliveries AS top_deliveries,
    ROUND((w.total_deliveries - t.total_deliveries) / t.total_deliveries * 100, 1) AS pct_diff_from_top
FROM warehouse_totals w
CROSS JOIN top_warehouse t
ORDER BY w.total_deliveries DESC;
