salix/print/templates/email/buyer-week-waste/sql/wasteWeekly.sql

14 lines
452 B
MySQL
Raw Normal View History

2024-05-10 09:05:20 +00:00
SELECT *, 100 * dwindle / total `percentage`
2021-04-06 11:46:38 +00:00
FROM (
2024-05-10 09:05:20 +00:00
SELECT u.name buyer,
SUM(saleTotal) total,
SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
2021-04-06 11:46:38 +00:00
FROM bs.waste w
2024-05-10 09:05:20 +00:00
JOIN account.user u ON u.id = w.buyerFk
JOIN vn.itemType it ON it.id = w.itemTypeFk
WHERE w.year = YEAR(util.VN_CURDATE() - INTERVAL 1 WEEK)
AND w.week = WEEK(util.VN_CURDATE() - INTERVAL 1 WEEK, 4)
GROUP BY buyerFk
2021-04-06 11:46:38 +00:00
) sub
2024-05-10 09:05:20 +00:00
ORDER BY `percentage` DESC;