简体   繁体   中英

Limiting results in SSRS SQL query

I am trying to build a report on our ticketing system using 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.

I am checking for tickets past SLA two ways:

1) the ticket is past the SLA date and there is no detail record matching the predetermined types
2) the ticket has a detail record matching the predetermined types but it was after the SLA date

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. 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. The SLA was 12/31/2015. The next day (1/2/2016) he also emails the TM using the EMAILOUT recordtype. The CTE returns both these rows/records. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM