mysql语句--分组查询

给定一个时间段,按照时间分组, 分组后,求分组的个数,应答率,平均等待时长(秒),平均通话时长(分钟)



SELECT
    tmp1.dateTime,tmp1.total ,ifnull(tmp2.total, 0) as answer,ROUND(ifnull(tmp2.total, 0)/tmp1.total*100,1),tmp1.answeravg ,tmp1.calltimeavg
FROM
    (
        SELECT truncate(AVG(callin.ANSWER_TIME - callin.CREATE_TIME)/1000,0) as answeravg,truncate(AVG(callin.FINISH_TIME - callin.ANSWER_TIME)/(1000*60),0) as calltimeavg,
            FROM_UNIXTIME(
                callin.CREATE_TIME,
                #"%Y-%m-%d %H:%m:%s"
                "%Y-%m-%d %H"
            ) AS dateTime,
            COUNT(ID) AS total
FROM
    AIIVR_CALLIN_RECORD callin
WHERE
    callin.CREATE_TIME >= 1223132422
AND callin.CREATE_TIME <= 1223312111
and callin.IS_DELETE = 0
GROUP BY dateTime
ORDER BY callin.CREATE_TIME ASC) tmp1 LEFT JOIN (SELECT
    FROM_UNIXTIME(
        callin.CREATE_TIME,
        #"%Y-%m-%d %H:%m:%s"
    "%Y-%m-%d %H"
    ) AS dateTime
,COUNT(*) as total
FROM
    AIIVR_CALLIN_RECORD callin
WHERE
    callin.CREATE_TIME >= 1223132422
AND callin.CREATE_TIME <= 1223312111
and callin.IS_DELETE = 0
and callin.CALL_STATE = 0
GROUP BY dateTime
ORDER BY callin.CREATE_TIME ASC) tmp2 on tmp1.dateTime=tmp2.dateTime

你可能感兴趣的:(mysql,语句)