SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY first_category ORDER BY first_category_sales DESC, second_category_sales DESC, third_category_sales DESC) AS row_num
FROM (
SELECT *, SUM(sales) OVER (PARTITION BY first_category) AS first_category_sales, SUM(sales) OVER (PARTITION BY first_category, second_category) AS second_category_sales, SUM(sales) OVER (PARTITION BY first_category, second_category, third_category) AS third_category_sales
FROM category_sales
) AS subquery
) AS final_query
WHERE row_num <= 10
ORDER BY first_category_sales DESC, second_category_sales DESC, third_category_sales DESC;