WITH job_categories AS (
    SELECT DISTINCT 
        CAST(JSON_EXTRACT(properties, '$.node_id') AS UNSIGNED) AS category_id,
        created_at AS job_created_at,
        JSON_UNQUOTE(JSON_EXTRACT(properties, '$.domain_id')) AS domain_id
    FROM job_statuses
    WHERE id >= 9712
        AND type = 'best_seller'
        AND status != 'failed'
        AND JSON_EXTRACT(properties, '$.node_id') IS NOT NULL
)
SELECT 
    p.id, 
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.fan_type')) AS fan_type,
    NULLIF(JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.decibels')), '0') AS decibels,
    NULLIF(JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.speeds')), '0') AS speeds,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.connectivity.has_connectivity')) AS has_connectivity,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.connectivity.connectivity_type')) AS connectivity_type,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.timer')) AS timer,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.oscillating')) AS oscillating,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.adjustable_tilt')) AS adjustable_tilt,
    NULLIF(JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.power')), '0') AS power,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.blade_material')) AS blade_material,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.blade_color')) AS blade_color,
    NULLIF(JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.diameter')), '0') AS diameter,
    NULLIF(JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.number_of_blades')), '0') AS number_of_blades,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.retractable_blades')) AS retractable_blades,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.integrated_lighting')) AS integrated_lighting,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.color_temperature')) AS color_temperature,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.energy_efficiency')) AS energy_efficiency,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.summer_winter_function')) AS summer_winter_function
FROM products p
JOIN job_categories jc ON p.category = jc.category_id
LEFT JOIN product_attributes pa ON p.id = pa.product_id
WHERE p.created_at >= jc.job_created_at
ORDER BY 
    CASE 
        WHEN JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.fan_type')) IS NULL THEN 1 
        ELSE 0 
    END,
    JSON_UNQUOTE(JSON_EXTRACT(pa.category_attributes, '$.fan_type'));