salix/db/changes/10300-newFacility/00-weekWaste_getDetail.sql

31 lines
713 B
MySQL
Raw Normal View History

2021-04-26 11:40:52 +00:00
DROP PROCEDURE IF EXISTS `bs`.`weekWaste_getDetail`;
2021-04-06 11:46:38 +00:00
2021-04-26 11:40:52 +00:00
DELIMITER $$
$$
CREATE
DEFINER = root@`%` PROCEDURE `bs`.`weekWaste_getDetail__`()
2021-04-06 11:46:38 +00:00
BEGIN
2021-04-26 11:40:52 +00:00
DECLARE vLastWeek DATE;
DECLARE vWeek INT;
2021-04-06 11:46:38 +00:00
DECLARE vYear INT;
2021-04-26 11:40:52 +00:00
SET vLastWeek = TIMESTAMPADD(WEEK, -1, CURDATE());
2021-04-06 11:46:38 +00:00
SET vYear = YEAR(vLastWeek);
SET vWeek = WEEK(vLastWeek, 1);
2021-04-26 11:40:52 +00:00
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 ;
2021-04-06 11:46:38 +00:00