Dit spørgsmål lader til at handle om at bestille resultaterne. Løsningen er at bruge vinduesfunktioner i 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;