SELECT item_id, warehouse_id, SUM(amount) amount FROM basket_item GROUP BY warehouse_id CALL bionic_from_type (@calc, #type); SELECT t.item_id, SUM(t.available) - IFNULL(m.amount, 0) available, MIN(t.price) price, a.Foto, a.Article, a.Categoria, a.Medida, a.Tallos, a.Color, o.Abreviatura FROM cache.bionic t JOIN vn2008.Articles a ON a.Id_Article = t.item_id LEFT JOIN vn2008.Origen o ON a.id_origen = o.id LEFT JOIN ( SELECT item_id, SUM(amount) amount FROM basket_item GROUP BY item_id ) m ON m.item_id = t.item_id WHERE t.calc_id = @calc AND t.available > 0 GROUP BY item_id ORDER BY a.Article, a.Medida LIMIT 400; CALL bionic_from_type (@calc, #type); SELECT l.warehouse_id, l.items, l.grouping, l.price, l.rate, t.amount, b.available - IFNULL(t.amount, 0) available FROM cache.bionic b LEFT JOIN ( SELECT warehouse_id, SUM(amount) amount FROM basket_item WHERE item_id = #item GROUP BY warehouse_id ) t ON t.warehouse_id = b.warehouse_id JOIN cache.bionic_lot l ON l.warehouse_id = b.warehouse_id AND l.item_id = b.item_id AND l.calc_id = @calc WHERE b.calc_id = @calc AND b.item_id = #item ORDER BY warehouse_id, grouping;