Uw vraag lijkt te gaan over het bestellen van de resultaten. De oplossing is om vensterfuncties te gebruiken in ORDER BY
:
SELECT lot, defect, SUM(quantity)
FROM table
GROUP BY lot, defect
ORDER BY SUM(SUM(quantity)) OVER (PARTITION BY lot) DESC,
lot, SUM(quantity) DESC, defect;