salix/db/changes/10140-kings/00-weekWaste_getDetail.sql

30 lines
654 B
MySQL
Raw Normal View History

2020-01-21 08:33:26 +00:00
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 ;