[英]Limiting results in SSRS SQL query
I am trying to build a report on our ticketing system using SSRS. 我正在尝试使用SSRS建立一个关于我们的票务系统的报告。 I am bringing in fields from both an "incident body" table and an "incident details" table. 我从“事件正文”表和“事故详情”表中引入字段。 What I am looking for is whether the ticket has slipped past an SLA for response to the customer. 我正在寻找的是机票是否已经超过SLA以响应客户。
I am checking for tickets past SLA two ways: 我正在检查过去SLA的门票有两种方式:
1) the ticket is past the SLA date and there is no detail record matching the predetermined types 1)票证超过SLA日期,并且没有与预定类型匹配的详细记录
2) the ticket has a detail record matching the predetermined types but it was after the SLA date 2)票证具有与预定类型匹配的详细记录,但是在SLA日期之后
I was able to build the CTE section below but I am struggling with how to reduce the result of that to just the oldest matching row/record. 我能够在下面构建CTE部分,但我正在努力将如何将其结果减少到最旧的匹配行/记录。 A ticket should only show up once in the report. 票证只应在报告中显示一次。
For example: 例如:
Johnny Blogs updates a ticket using the CONTACTED_TM recordtype on 1/1/2016. Johnny Blogs使用2016年1月1日的CONTACTED_TM记录类型更新票证。 The SLA was 12/31/2015. SLA是2015年12月31日。 The next day (1/2/2016) he also emails the TM using the EMAILOUT recordtype. 第二天(2016年1月),他还使用EMAILOUT记录类型向TM发送电子邮件。 The CTE returns both these rows/records. CTE返回这两行/记录。 I only want the first result. 我只想要第一个结果。
My query code is below. 我的查询代码如下。 I have tried using a SELECT TOP 1 in the CTE, but it just causes it to return no results. 我尝试在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'
Something like this, as referenced in the comments. 像评论中引用的那样的东西。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.