我有这个表,其中包含员工的姓名和他们在mysql中的电话持续时间。该表如下所示:
Caller Emplid Calldate Call_Duration
Jack 333 1/1/2016 43
Jack 333 1/2/2016 45
Jack 333 1/3/2016 87
Jack 333 2/4/2016 44
Jack 333 2/5/2016 234
jack 333 2/6/2016 431
Jeff 111 1/1/2016 23
Jeff 111 1/2/2016 54
Jeff 111 1/3/2016 67 48
我正在尝试按月计算每个员工total_Duration的每日平均运行时间。假设我有4月的每日运行平均值,那么5月的运行平均值应该从5月1日开始到当月31日结束。我尝试过很多方法,mysql没有像sql server那样的pivot和分区功能。每天拨打电话的员工总数都会发生变化,我需要能够动态照顾不会打电话的员工。
输出应如下所示:
Caller Emplid Calldate Call_Duration Running_avg
Jack 333 1/1/2016 43 43
Jack 333 1/2/2016 45 44
Jack 333 1/3/2016 87 58.33333333
Jack 333 2/4/2016 44 44
Jack 333 2/5/2016 234 139
Jack 333 2/6/2016 431 236.3333333
Jeff 111 1/1/2016 23 23
Jeff 111 1/2/2016 54 38.5
Jeff 111 1/3/2016 67 48
这是我在下面开始的查询:
SELECT row_number,Month_Year,Callername,Calldate,Caller_Emplid,`Sum of Call`,`Sum of Call`/row_number as AvgCall,
@`sum of call`:=@`sum of call`+ `sum of call` overallCall,
@row_number:=row_number overallrow_number,
@RunningTotal:=@`sum of call`/@row_number runningTotal
FROM
(SELECT
@row_number:=
CASE WHEN @CallerName=CallerName and date_format(calldate,'%d') = date_format(calldate,'%d') and
date_format(calldate,'%m') = date_format(calldate,'%m')
THEN @row_number+1
ELSE 1 END AS row_number,@CallerName:=CallerName AS Callername,Calldate,Caller_Emplid,Month_Year,`Sum of Call`
FROM transform_data_2, (SELECT @row_number:=0,@CallerName:='') AS t
ORDER BY callername) a
JOIN (SELECT @`Sum of call`:= 0) t