找到别名的总和然后分组

时间:2013-08-14 21:24:56

标签: mysql sum alias

我知道直接对别名进行计算是不可能的,但有没有办法可以找到别名的总和(总时间)?感谢您的帮助。

    SELECT *,
         TIMEDIFF( time2, time1 ) ,
         TIMEDIFF( time4, time3 ) ,
         ADDTIME( TIMEDIFF( time2, time1 ) ,
         TIMEDIFF( time4, time3 ) ) ,
         TIME_FORMAT( ADDTIME( TIMEDIFF( time2, time1 ) ,
         TIMEDIFF( time4, time3 ) ) , '%H:%i' )
    AS totaltime
        SUM(SELECT(totaltime)) AS sumtotal
    FROM
        timer
    WHERE
        groupid='100'
    AND
        MONTH(`date`) = MONTH(NOW())
    GROUP BY userid

1 个答案:

答案 0 :(得分:0)

只需添加另一层抽象;)

SELECT TIME_FORMAT(sec_to_time(sum(timecard.totaltime)),'%H:%i' ) FROM 
(
SELECT *,
    TIMEDIFF( out_time, in_time ) , 
    TIMEDIFF( lunch_in, lunch_out ) , 
    ADDTIME( TIMEDIFF( out_time, in_time ) , TIMEDIFF( lunch_in, lunch_out ) ) , 
    ADDTIME( TIMEDIFF( out_time, in_time ) , 
    TIMEDIFF( lunch_in, lunch_out ) )
AS totaltime        
FROM
    timecard
WHERE
    comp_id='129'
AND
    MONTH(`date`) = MONTH(NOW())
GROUP BY emp_id
) timecard

链接到小提琴:http://sqlfiddle.com/#!2/b33d8/16/0