2021-04-26 11:40:52 +00:00
|
|
|
DROP PROCEDURE IF EXISTS `bs`.`weekWaste_byWorker`;
|
2021-04-06 11:46:38 +00:00
|
|
|
|
2021-04-26 11:40:52 +00:00
|
|
|
DELIMITER $$
|
|
|
|
$$
|
|
|
|
CREATE
|
|
|
|
DEFINER = root@`%` PROCEDURE `bs`.`weekWaste_byWorker__`(IN vWorkerFk INT)
|
2021-04-06 11:46:38 +00:00
|
|
|
BEGIN
|
|
|
|
|
2021-04-26 11:40:52 +00:00
|
|
|
DECLARE vWeek INT;
|
2021-04-06 11:46:38 +00:00
|
|
|
DECLARE vYear INT;
|
2021-04-26 11:40:52 +00:00
|
|
|
|
2021-04-06 11:46:38 +00:00
|
|
|
SELECT week, year
|
2021-04-26 11:40:52 +00:00
|
|
|
INTO vWeek, vYear
|
|
|
|
FROM vn.time
|
|
|
|
WHERE dated = TIMESTAMPADD(WEEK, -1, CURDATE());
|
2021-04-06 11:46:38 +00:00
|
|
|
|
2021-04-26 11:40:52 +00:00
|
|
|
SELECT *, 100 * mermas / total AS porcentaje
|
|
|
|
FROM (
|
|
|
|
SELECT ws.family,
|
|
|
|
SUM(ws.saleTotal) AS total,
|
|
|
|
SUM(ws.saleWaste) AS mermas
|
|
|
|
FROM bs.waste ws
|
2021-04-06 11:46:38 +00:00
|
|
|
JOIN vn.worker w ON w.user = ws.buyer
|
2021-04-26 11:40:52 +00:00
|
|
|
WHERE year = vYear
|
|
|
|
AND week = vWeek
|
|
|
|
AND w.id = vWorkerFk
|
|
|
|
GROUP BY family
|
|
|
|
) sub
|
|
|
|
ORDER BY porcentaje DESC;
|
|
|
|
END;;$$
|
|
|
|
DELIMITER ;
|
2021-04-06 11:46:38 +00:00
|
|
|
|