सबसे पहले, आपको उप प्रश्नों की आवश्यकता नहीं है, आप इसके बजाय एक शर्त पर गणना कर सकते हैं।
with rollup
संशोधक को group by में जोड़ा जा सकता है कुल योग को शामिल करने के लिए खंड। order by तब उसी क्वेरी में उपयोग नहीं किया जा सकता है, लेकिन बाहरी क्वेरी में लागू किया जा सकता है।
इसके अलावा, coalesce . के उपयोग के साथ आप null . को बदल सकते हैं अपनी पसंद के लेबल के साथ उस कुल पंक्ति के लिए मान।
अंत में, अभी भी अंत में कुल पंक्ति को क्रमबद्ध करने के लिए, आप एक is null add जोड़ सकते हैं order by . में व्यंजक क्लॉज, जो false . का मूल्यांकन करेगा या true . बाद वाला अंतिम आदेश दिया गया है।
select coalesce(checkby, 'Total') as checkby_or_total,
fully,
faulty,
lasthour,
total
from (
select qcheck.checkby,
count(case result when 'fully tested & working' then 1 end) as fully,
count(case result when 'faulty' then 1 end) as faulty,
count(case when finishdate >= now()-interval 1 hour then 1 end) as lasthour,
count(*) as total
from qcheck
where date(finishdate) = CURDATE()
and qcheck.checkby not like 'michael'
and qcheck.checkby not like 'chaz'
group by qcheck.checkby with rollup
) as main
order by checkby is null,
total desc