SELECT
c.category_id,
cd.category,
b.brand,
b.brand_id,
COUNT(DISTINCT p.product_id) AS product_count
FROM
cscart_products p
JOIN cscart_products_categories pc ON pc.product_id = p.product_id
JOIN cscart_categories c ON pc.category_id = c.category_id
JOIN cscart_category_descriptions cd ON c.category_id = cd.category_id
JOIN cscart_brands b ON p.brand_id = b.brand_id
WHERE
c.id_path LIKE '%712%'
AND p.status = 'A'
GROUP BY
b.brand
ORDER BY
product_count DESC
LIMIT
16;