堆栈溢出神,
在 BMC Remedy 中,没有任何开箱即用的功能可以让您查看一张票在某人的队列中排了多长时间。我创建了一个执行此操作的 SQL 函数,但速度很慢。你能帮我调整一下或者重新考虑我的流程吗?
故事数据看起来像这样http://i.stack.imgur.com/B4Uzu.png
所以基本上我正在做的是计算每个“价值”字段之间的差异(日期在纪元中)。我得到前一行和当前行之间的差异(以秒为单位)然后除以 60 给我分钟
ALTER function [dbo].[getTimeAssigned]
(
@support_group nvarchar(100),
@request_id nvarchar(100)
)
returns bigint
as
begin
--Sample data that would be sent to the function
/*
declare @request_id nvarchar(30);
declare @support_group nvarchar(50);
set @support_group = 'Security Support';
set @request_id = 'INC000001049252';
*/
--can't do temp tables inside a function so I made it a variable
declare @tempTable table
(
request_id nvarchar(30)
, assigned_group nvarchar(100)
, minutes_assigned bigint
)
--need to find if ticket has been closed or not
--if not we want to apply current timestamp to 'resolution_time' because i want to see how long it has been open
declare @resolution_time bigint;
select @resolution_time = audit_date
from HPD_HELPDESK_AUDITLOGSYSTEM_V
where ORIGINAL_REQUEST_ID = @request_id
and ([LOG] like '%resolution: resolved%' or [LOG] like '%status: Resolved%')
--means that the ticket is still open so apply current timestamp to resolution time
if (@resolution_time is null) set @resolution_time = DATEDIFF(s, '1970-01-01 00:00:00', GETDATE())+(3600*5)
--slow part
--need to calculate the time difference between the previous row's assign time and the current rows assign time
--that would give me how long it spent in each queue
;with story as
(
select rownum = ROW_NUMBER() over (order by audit_date)
, [LOG]
, ORIGINAL_REQUEST_ID
, AUDIT_DATE [value]
from HPD_HELPDESK_AUDITLOGSYSTEM_V
where ORIGINAL_REQUEST_ID = @request_id and [LOG] like '%assigned group%'
)
--inserting into temp table to be able to aggregate only the amount assigned to @support_group and return that value
insert into @tempTable
select prev.ORIGINAL_REQUEST_ID
, case
--just renames this field to the support group i'm looking for
when prev.[LOG] like '%assigned group: ' + @support_group + '%' then @support_group
--any other group that i don't care about
else 'Another Group'
end [Assigned_Group]
, (cur.value - prev.value) / 60 [minutes_assigned] --THE RUB
from story cur
inner join story prev
on prev.rownum = cur.rownum - 1
union all
select cur.ORIGINAL_REQUEST_ID
, case
when cur.[LOG] like '%assigned group: ' + @support_group + '%' then @support_group
else 'Another Group'
end [Assigned_Group]
, (@resolution_time - cur.value)/60 [minutes_assigned] --THE RUB!!!
from story cur
where cur.rownum = (select MAX(story.rownum) from story)
declare @return bigint
--aggregating for return
select @return = SUM(minutes_assigned)
from @tempTable
where assigned_group = @support_group
group by assigned_group, request_id
return @return
end
如果我在过去 30 天内针对票运行此操作,则大约需要 1 1/2 小时才能完成。
我哪里错了?它是表变量吗?我可以做些什么来加快速度?
我相信 SQL 2012 有一个内置的 prev/next 回调,但 Remedy 在 2008 年,所以我必须手动执行 - 我对此是否正确?