CREATE VIEW stock_current_location_content AS SELECT IFNULL(s.location_id, p.location_id) AS location_id, s.product_id, SUM(s.amount) AS amount, MIN(s.best_before_date) AS best_before_date, IFNULL((SELECT SUM(amount) FROM stock WHERE product_id = s.product_id AND location_id = s.location_id AND open = 1), 0) AS amount_opened FROM stock s JOIN products p ON s.product_id = p.id GROUP BY IFNULL(s.location_id, p.location_id), s.product_id;