 WHERE sub_p.min_stock_amount != 0
 	AND p.cumulate_min_stock_amount_of_sub_products = 1
 GROUP BY p.id
-HAVING IFNULL(SUM(s.amount), 0) < SUM(sub_p.min_stock_amount)
+HAVING IFNULL(SUM(s.amount_aggregated), 0) < SUM(sub_p.min_stock_amount)
 
 UNION
 

WHERE sub_p.min_stock_amount != 0
	AND p.cumulate_min_stock_amount_of_sub_products = 1
GROUP BY p.id
HAVING IFNULL(SUM(s.amount), 0) < SUM(sub_p.min_stock_amount)

UNION


SELECT
	p.id,
	MAX(p.name) AS name,
	SUM(sub_p.min_stock_amount) - (IFNULL(SUM(s.amount_aggregated), 0) - IFNULL(SUM(s.amount_opened), 0)) AS amount_missing,
	CASE WHEN IFNULL(SUM(s.amount), 0) > 0 THEN 1 ELSE 0 END AS is_partly_in_stock
FROM products_view p
JOIN products_resolved pr
