在最后一行总计查询

时间:2016-06-18 14:56:12

标签: mysql

此查询将返回工程师名称列表,其中包含他们在过去一小时内测试过的测试结果,错误,工作内容以及每位工程师的总数。

我希望能够在底部添加一行来累计这些金额,但是我在苦苦挣扎,任何人都有任何建议吗?

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

1 个答案:

答案 0 :(得分:1)

首先,您不需要子查询,而是可以计算条件。

可以将with rollup修饰符添加到presence子句中以包含总计。 group by不能在同一查询中使用,但可以在外部查询中应用。

此外,使用order by,您可以使用您选择的标签替换该总行的coalesce值。

最后,要对结尾处的总行进行排序,您可以在null子句中添加is null表达式,该表达式将评估为order byfalse。后者是最后订购的。

true