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

14 lines
452 B
SQL

SELECT *, 100 * dwindle / total `percentage`
FROM (
SELECT u.name buyer,
SUM(saleTotal) total,
SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
FROM bs.waste w
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
) sub
ORDER BY `percentage` DESC;