Create a view category_stock with one row per category: products (how many products) and total_stock.
category_stock
category
products
total_stock
Run your code to see the output here.