SELECT
    v.PIN,
    p.manufacturer,
    MAX(CASE WHEN v.VendorCode = 'MRC' THEN v.VendorCode END) AS MRC,
    MIN(CASE WHEN v.VendorCode = 'MRC' THEN v.Cost END) AS MRC_cost,
    MAX(CASE WHEN v.VendorCode = 'ENC' THEN v.VendorCode END) AS ENC,
    MIN(CASE WHEN v.VendorCode = 'ENC' THEN v.Cost END) AS ENC_cost,
    MAX(CASE WHEN v.VendorCode = 'UED' THEN v.VendorCode END) AS UED,
    MIN(CASE WHEN v.VendorCode = 'UED' THEN v.Cost END) AS UED_cost,
    MAX(CASE WHEN v.VendorCode = 'DEY' THEN v.VendorCode END) AS DEY,
    MIN(CASE WHEN v.VendorCode = 'DEY' THEN v.Cost END) AS DEY_cost,
    MIN(v.Cost) AS cheapest_cost,
    MAX(cv.VendorCode) AS cheapest_vendor_code
FROM products.vendor_map v
INNER JOIN products.pins p ON v.PIN = p.PIN
INNER JOIN (
    SELECT PIN, MIN(Cost) AS cheapest_cost, VendorCode
    FROM products.vendor_map
    WHERE VendorCode IN ('MRC', 'ENC', 'UED', 'DEY')
    GROUP BY PIN, VendorCode
) cv ON v.PIN = cv.PIN AND v.Cost = cv.cheapest_cost
WHERE v.VendorCode IN ('MRC', 'ENC', 'UED', 'DEY')
GROUP BY v.PIN, p.manufacturer