【问题标题】:obtain an average of days an issue is open获取问题开放的平均天数
【发布时间】:2012-03-02 01:08:24
【问题描述】:

我有一个表格,其中包含我转换的“通话开放日期”列 (actual_log_date),使用

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

例如显示 2012 年 1 月 2 日登录的通话。

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

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

显示 2012 年 12 月 2 日结束的通话。

现在,我需要生成一个脚本来获取电话已打开的平均天数。我可以跑

(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')

再次感谢!

【问题讨论】:

  • 欢迎使用 StackOverflow:如果您发布代码、XML 或数据示例,在文本编辑器中突出显示这些行并单击“代码示例”按钮 ({ } ) 在编辑器工具栏上以很好地格式化和语法突出显示它!
  • 您如何处理尚未解决的问题?
  • 未关闭的调用不会被拉入脚本。

标签: sql sql-server-2008 average


【解决方案1】:

您可以使用 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

【讨论】:

  • 谢谢你,但忘了说我没有插入临时表的能力 - 我只有这个数据库的只读权限,因为它不属于我们,我们只能查询它:o (
  • 我只使用临时表来展示查询的实际工作方式。您可以使用 with 语句而无需创建临时表。
  • 谢谢杰 - 这让我得到了我的平均水平:o)
猜你喜欢
  • 2015-04-03
  • 1970-01-01
  • 1970-01-01
  • 2013-01-01
  • 2019-01-14
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多