Sql 获取问题开放的平均天数
我有一个带有“调用打开日期”列(实际日志日期)的表,我使用Sql 获取问题开放的平均天数,sql,sql-server-2008,average,Sql,Sql Server 2008,Average,我有一个带有“调用打开日期”列(实际日志日期)的表,我使用 convert(char(10), actual_log_date, 103) 'Call Logged' 例如,显示2012年2月1日记录的通话 还有一个close time列(close_time),我对其运行相同的转换 convert(char(10), close_time, 103) 'Call Closed' 显示2012年2月12日结束的通话 现在,我需要生成一个脚本,以获得打开呼叫的平均天数。我能跑 (select
convert(char(10), actual_log_date, 103) 'Call Logged'
例如,显示2012年2月1日记录的通话
还有一个close time列(close_time),我对其运行相同的转换
convert(char(10), close_time, 103) 'Call Closed'
显示2012年2月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')
再次感谢 您可以使用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
欢迎使用StackOverflow:如果您发布代码、XML或数据示例,请在文本编辑器中突出显示这些行,并单击编辑器工具栏上的“代码示例”按钮(
{}
),以精确地格式化和语法突出显示它!您如何处理尚未关闭的问题?未关闭的调用不会被拉入脚本中。谢谢您的帮助,但忘了说我没有插入临时表的能力-我对此数据库只有只读权限,因为它不是我们所有的,我们只能查询它:o(我只使用临时表来显示查询的实际工作方式。您可以使用with语句而无需创建临时表。谢谢Jay-这让我得到了我的平均值:o)