我正在尝试使用SSR在我们的票务系统上构建报告。我正在从"事件主体"表和"事件细节"表中带来田野。我正在寻找的是票是否滑过SLA以响应客户。
我正在检查SLA的门票两种方式:
1)票证已超过SLA日期,没有详细记录与预定类型匹配
2)门票具有与预定类型相匹配的详细记录,但在SLA日期之后
我能够构建下面的CTE部分,但我正在努力如何将结果减少到最古老的匹配行/记录。报告中只能在报告中出现一次。
例如:
Johnny博客在2016年1月1日使用Contacted_TM RecordType更新票证。SLA是2015年12月31日。第二天(2016年1月2日),他还使用EmailOut RecordType向TM发送电子邮件。CTE返回这两个行/记录。我只想要第一个结果。
我的查询代码在下面。我尝试在CTE中使用选择的顶级1,但这只是导致它没有返回结果。
WITH CTE (Date, [Action ID], Description, Note, [Login ID], [Seq.Group], [Incident #], ResponseDue) AS
(
SELECT Date, [Action ID], [Incident Details].Description, [Incident Details].Note, [Login ID], [Incident Details].[Seq.Group], [Incident Details].[Incident #], [Incident].[Due Date & Time:] as ResponseDue
FROM [_SMDBA_].[Incident Details]
JOIN [_SMDBA_].[Incident]
ON [Incident Details].[Incident #] = [Incident].[Incident #]
WHERE ([Action ID] = 'CONTACTED_TM' OR [Action ID] = 'TM_UNAVAILABLE' OR ([Action ID] = N'EMAILOUT' AND _SMDBA_.[Incident].[Client Email] in ([Email To Email From])))
--AND Date >= Incident.[Due Date & Time:]
)
SELECT
I.[Incident #]
,I.[Group Name]
,I.[Seq.Group] as IncidentGroupSeq
,I.[Due Date & Time:] as ResponseDue
,I.[Priority ID:]
,I.[Priority Duration]
,I.[Subject ID]
,I.[Open Date & Time] as OpenDate
,I.[Impact ID:] as Impact
,I.[Urgency ID:] as Urgency
,CTE.[Action ID]
,CTE.Description
,CTE.Note
,CTE.[Login ID]
,CTE.Date AS IncidentDetailsDate
,CTE.[Seq.Group] as DetailsGroupSeq
,_SMDBA_.CalcWorkingSeconds(1001,[Due Date & Time:],CTE.Date) as WorkingSecs
,CASE
WHEN CTE.date >= I.[Due Date & Time:] THEN 'Response was Late'
WHEN CTE.Date IS NULL THEN 'There was no response'
WHEN CTE.date <= I.[Due Date & Time:] THEN 'Met SLA'
WHEN I.[Due Date & Time:] IS NULL THEN 'No Response date set'
END AS SLAStatus
FROM [_SMDBA_].Incident I
LEFT OUTER JOIN CTE
ON CTE.[Incident #] = I.[Incident #]
WHERE
I.[Open Date & Time] >= @StartDate
AND I.[Open Date & Time] <= @EndDate
AND I.[Group Name] in (@Group)
AND I.[Impact ID:] in (@Impact)
AND I.[Urgency ID:] in (@Urgency)
AND I.[Opened Group:] = 'SERVICE DESK'
如评论中所述。
WITH CTE ([Date], [Action ID], Description, Note, [Login ID], [Seq.Group], [Incident #], ResponseDue) AS
(
SELECT
[Date],
[Action ID],
[Incident Details].Description,
[Incident Details].Note,
[Login ID],
[Incident Details].[Seq.Group],
[Incident Details].[Incident #],
[Incident].[Due Date & Time:] as ResponseDue,
row_number() over (partition by [Incident Details].[Incident #] order by [Date] desc) as RN
FROM [_SMDBA_].[Incident Details]
JOIN
[_SMDBA_].[Incident]
ON [Incident Details].[Incident #] = [Incident].[Incident #]
WHERE
([Action ID] = 'CONTACTED_TM' OR [Action ID] = 'TM_UNAVAILABLE' OR
([Action ID] = N'EMAILOUT' AND _SMDBA_.[Incident].[Client Email] in ([Email To Email From])))
--AND Date >= Incident.[Due Date & Time:]
)
SELECT
I.[Incident #]
,I.[Group Name]
,I.[Seq.Group] as IncidentGroupSeq
,I.[Due Date & Time:] as ResponseDue
,I.[Priority ID:]
,I.[Priority Duration]
,I.[Subject ID]
,I.[Open Date & Time] as OpenDate
,I.[Impact ID:] as Impact
,I.[Urgency ID:] as Urgency
,CTE.[Action ID]
,CTE.Description
,CTE.Note
,CTE.[Login ID]
,CTE.Date AS IncidentDetailsDate
,CTE.[Seq.Group] as DetailsGroupSeq
,_SMDBA_.CalcWorkingSeconds(1001,[Due Date & Time:],CTE.Date) as WorkingSecs
,CASE
WHEN CTE.date >= I.[Due Date & Time:] THEN 'Response was Late'
WHEN CTE.Date IS NULL THEN 'There was no response'
WHEN CTE.date <= I.[Due Date & Time:] THEN 'Met SLA'
WHEN I.[Due Date & Time:] IS NULL THEN 'No Response date set'
END AS SLAStatus
FROM [_SMDBA_].Incident I
LEFT OUTER JOIN CTE
ON CTE.[Incident #] = I.[Incident #]
WHERE
I.[Open Date & Time] >= @StartDate
AND I.[Open Date & Time] <= @EndDate
AND I.[Group Name] in (@Group)
AND I.[Impact ID:] in (@Impact)
AND I.[Urgency ID:] in (@Urgency)
AND I.[Opened Group:] = 'SERVICE DESK'
AND CTE.RN = 1