SELECT t1.id as id, MAX(t3.value) as max, MIN(t3.value) as min
FROM param t1
  LEFT JOIN paramCategory t2 ON t2.paramId = t1.id
  LEFT JOIN paramValueInt t3 ON t3.paramId = t1.id
  LEFT JOIN paramValueProduct t4 ON t4.paramValueId = t3.id
WHERE (
  t1.type = 'integer'
  AND t2.categoryId = %d
  AND t4.productId IN (
    SELECT t_1.id
    FROM product as t_1
    LEFT JOIN productCategory as t_2 ON t_2.productId = t_1.id
    LEFT JOIN factorySaleDepartment as t_3 ON t_3.factory = t_1.factoryId
    WHERE (
      t_2.categoryId = %d
      AND t_3.saleDepartment = %d
    )
  )
)
GROUP BY t1.id
