获得问题开放的平均天数

时间:2012-03-01 14:48:04

标签: sql sql-server-2008 average

我有一个带有'call open date'列的表(actual_log_date),我将其转换为

convert(char(10), actual_log_date, 103) 'Call Logged'

显示例如01/02/2012上记录的呼叫。

还有一个关闭时间列(close_time),我运行相同的转换,

convert(char(10), close_time, 103) 'Call Closed' 

显示12月12日结束的通话。

现在,我需要制作一个脚本来获取通话打开的平均天数。我可以跑

(select datediff(dd, c.actual_log_date, c.close_time)) as 'Days Open' 

创建显示调用已打开12天或其他任何内容的列,但现在需要计算所有记录并获得结果末尾的平均总和,这就是我被卡住的地方!

我忘了说我不能创建临时表,因为我只有权限。

对不起,我是新来的,所以不确定协议,但是这里是脚本,如果它更容易回答我正在使用的内容;

select 

convert(char(10),actual_log_date,103) 'Call Logged',
call_number 'Call #', 
short_problem 'Issue Description',
Customer = (select surname + ', ' + first_name from i.ar_user_attributes where ref = user_ref ),
Officer  = (select surname + ', ' + first_name from i.su_help_centre where ref = resolve_officer),
Department = (select name from i.su_support_group where ref = resolve_group),
convert(char(10),close_time,103) 'Closed',
(select datediff(hh,c.actual_log_date,c.close_time)) as 'Hours Open',
(select datediff(dd,c.actual_log_date,c.close_time)) as 'Days Open'

from 
i.cl_call_logging c

where 
c.resolve_time between
convert(datetime,convert(varchar,month(dateadd(m,-1,getdate()))) + '/01/' + convert(varchar,year(dateadd(m,-1,getdate())))) and
convert(datetime,convert(varchar,month(getdate())) + '/01/' + convert(varchar,year(getdate())))
and c.resolve_group in ('48', '60')

再次感谢!

1 个答案:

答案 0 :(得分:0)

您可以使用with查询,如下所示:

insert into @temp (CallOpen, CallClosed)
values
('2012-01-02', '2012-02-12'),
('2012-01-05', '2012-02-12'),
('2012-01-07', '2012-02-05'),
('2012-01-14', '2012-02-03'),
('2012-02-10', '2012-03-15')

;with T1 as 
(
select
    CallOpen,
    CallClosed,
    DATEDIFF(dd, callopen, callclosed) as DaysOpen
from @temp
)

select AVG(daysopen) from T1

使用完整脚本,试试这个:

;with T1 as
(
select 
    convert(char(10),actual_log_date,103) 'Call Logged',
    call_number 'Call #', 
    short_problem 'Issue Description',
    Customer = (select surname + ', ' + first_name from i.ar_user_attributes where ref = user_ref ),
    Officer  = (select surname + ', ' + first_name from i.su_help_centre where ref = resolve_officer),
    Department = (select name from i.su_support_group where ref = resolve_group),
    convert(char(10),close_time,103) 'Closed',
    (select datediff(hh,c.actual_log_date,c.close_time)) as 'Hours Open',
    (select datediff(dd,c.actual_log_date,c.close_time)) as 'Days Open'
from @cl_call_logging c
where 
    c.resolve_time between
        convert(datetime,convert(varchar,month(dateadd(m,-1,getdate()))) + '/01/' + convert(varchar,year(dateadd(m,-1,getdate())))) and
        convert(datetime,convert(varchar,month(getdate())) + '/01/' + convert(varchar,year(getdate())))
    and c.resolve_group in ('48', '60')
)

select AVG([Days Open]) from T1