drop procedure weekWaste_getDetail; create definer = root@`%` procedure weekWaste_getDetail__() BEGIN DECLARE vLastWeek DATE; DECLARE vWeek INT; DECLARE vYear INT; SET vLastWeek = TIMESTAMPADD(WEEK,-1,CURDATE()); SET vYear = YEAR(vLastWeek); SET vWeek = WEEK(vLastWeek, 1); SELECT *, 100 * dwindle / total AS percentage FROM ( SELECT buyer, ws.family, sum(ws.saleTotal) AS total, sum(ws.saleWaste) AS dwindle FROM bs.waste ws WHERE year = vYear AND week = vWeek GROUP BY buyer, family ) sub ORDER BY percentage DESC; END;