USE `bs`; DROP procedure IF EXISTS `weekWaste_getDetail`; DELIMITER $$ USE `bs`$$ 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$$ DELIMITER ;