i made a stored procedure that outputs the total number of items sold during the last three years for the P1 Product ID. Input the ProductID when invoking the procedure.
DELIMITER //
CREATE PROCEDURE evaluateProduct(
IN input_productid VARCHAR(10),
OUT total_sold_2020 INT,
OUT total_sold_2021 INT,
OUT total_sold_2022 INT,
OUT total_sold_overall INT
)
BEGIN
— Calculate totals for each year and overall total
SELECT
SUM(CASE WHEN YEAR(date) = 2020 THEN quantity ELSE 0 END),
SUM(CASE WHEN YEAR(date) = 2021 THEN quantity ELSE 0 END),
SUM(CASE WHEN YEAR(date) = 2022 THEN quantity ELSE 0 END),
SUM(quantity)
INTO total_sold_2020, total_sold_2021, total_sold_2022, total_sold_overall
FROM orders
WHERE productID = input_productid
AND date >= DATE_SUB(CURDATE(), interval 3 year);
END //
DELIMITER ;
call evaluateProduct(‘p1’, @total_sold_2020, @total_sold_2021, @total_sold_2022, @total_sold_overall);
select @total_sold_2020, @total_sold_2021, @total_sold_2022, @total_sold_overall;
when i run this i got 0 for @total_sold_2020 and when i individually calculates
select sum(quantity)
from orders
where productid=’p1′ and year(date)=2020;
i got answer 35 which is correct
[enter image description here]enter image description here(https://i.sstatic.net/BNQ0cozu.png)Iz.png)
i want to expect correct answe 35 for year 2020 when i run stored procedure