dimanche 19 juin 2016

Totalling query in last row

This query will return a list of engineer names with test results for what they have tested in the last hour, what is faulty, what's is working and the total for each engineer.

I want to be able to add a row at the bottom which will total these amounts but am struggling, any one have any suggestions?

select distinct qcheck.checkby,
                ifnull(fully,0)    as fully,
                ifnull(faulty,0)   as faulty,
                ifnull(lasthour,0) as lasthour,
                ifnull(total,0)    as total 
from            qcheck
left join       (
                select   count(*) AS fully,
                         checkby,
                         qcheck.id 
                from     qcheck
                where    result = 'fully tested & working' 
                and      date(finishdate) = CURDATE() 
                group by checkby) AS fw 
             on fw.checkby=qcheck.checkby
left join (
                select   count(*) AS faulty,
                         checkby,
                         qcheck.id 
                from     qcheck
                where    result = 'faulty' 
                and      date(finishdate) = CURDATE() 
                group by checkby) AS ff 
             on ff.checkby=qcheck.checkby
left join (
                select   count(*) AS Lasthour,
                         checkby,
                         qcheck.id from qcheck
                where    finishdate >= now() - interval 1 hour 
                group by checkby) AS lh 
             on lh.checkby=qcheck.checkby
left join (
                select   count(*) AS total,
                         checkby,
                         qcheck.id from qcheck
                where    date(finishdate) = CURDATE() 
                group by checkby) AS total 
             on total.checkby=qcheck.checkby
where           date(finishdate) = CURDATE() 
and             qcheck.checkby not like 'michael' 
and             qcheck.checkby not like 'chaz'
group by        qcheck.checkby
order by        total desc

Aucun commentaire:

Enregistrer un commentaire