Pertanyaan Anda tampaknya tentang memesan hasil. Solusinya adalah dengan menggunakan fungsi jendela di 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;