2016-12-28 177 views
1

我正在嘗試使用SSRS在我們的票務系統上生成報告。我正在從「事件機構」表和「事件詳細信息」表中引入字段。我在尋找的是該票是否已經滑過SLA以迴應客戶。在SSRS SQL查詢中限制結果

我檢查買票過去SLA兩種方式:

1)門票是過去的SLA日期並沒有相匹配的預定類型
2)票有詳細記錄相匹配的預定詳細記錄類型,但它是在SLA日期之後

我能夠在下面構建CTE部分,但我正在努力如何將結果減少到最早的匹配行/記錄。一張票只能在報告中顯示一次。

例如:

約翰尼更新博客上使用2016年1月1日的CONTACTED_TM RECORDTYPE票。 SLA是12/31/2015。第二天(1/2/2016)他還使用EMAILOUT記錄類型向TM發送電子郵件。 CTE返回這兩行/記錄。我只想要第一個結果。

我的查詢代碼如下。我曾嘗試在CTE中使用SELECT TOP 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' 
+0

定義「** First **」。你需要在CTE上添加諸如'row_numer()之類的東西(由I [按照日期描述的[事件#]順序)'作爲RN',然後在你選擇的底部使用'where RN = 1' CTE – scsimon

回答

0

就像這樣,在評論中引用。

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 
+0

這似乎工作,謝謝!我對'AND CTE.RN = 1'做了一個小改動,在那裏我改成了:AND(CTE.RN = 1或CTE.RN爲NULL)',在我看來,它應該只提供最晚的第一條記錄響應或者沒有響應的記錄。 – errational