限制SSRS SQL查询中的结果

时间:2016-12-28 21:35:07

标签: sql sql-server ssrs-2008-r2 common-table-expression

我正在尝试使用SSRS建立一个关于我们的票务系统的报告。我从“事件正文”表和“事故详情”表中引入字段。我正在寻找的是机票是否已经超过了SLA以响应客户。

我正在通过SLA两种方式检查门票:

1)票证超过SLA日期,没有与预定类型相匹配的明细记录 2)票证具有与预定类型匹配的详细记录,但它是在SLA日期之后

我能够在下面构建CTE部分但我正在努力将如何将其结果减少到最旧的匹配行/记录。票证只应在报告中显示一次。

例如:

Johnny Blogs使用2016年1月1日的CONTACTED_TM记录类型更新故障单。 SLA是2015年12月31日。第二天(2016年1月),他还使用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'

1 个答案:

答案 0 :(得分: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