DROP PROCEDURE IF EXISTS `bs`.`weekWaste_getDetail`; DELIMITER $$ $$ CREATE DEFINER = root@`%` PROCEDURE `bs`.`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 ;