Capstone: explain a ticket SLA report
Build an auditable event-to-SLA report with staged CTEs, JSON extraction, `LAG`, window elapsed time, grouped ticket outcomes, and within-team ranking.
What you are trying to do
An operations team wants to identify late tickets while retaining enough event history to explain each result. This synthetic, read-only capstone runs in one SQL Server session.
Work through it
- Start at
EventLog. Trace each event, its ticket, team, SLA limit, and payload owner. - In
Annotated, follow the prior event withLAG, extract the owner from the JSON payload, and measure elapsed minutes from the first event for each ticket. - In
TicketSummary, collapse event rows to one row per ticket, count state changes, and select its resolved time.Classifiedlabels open, late, and within-SLA tickets. - In
Ranked, rank late and longer-running tickets inside each team. Check the filter and ties before using a rank as a report cutoff. - Explain why ticket 101 is late at 70 minutes, ticket 102 is within its 30-minute limit, and ticket 201 resolves exactly at its 45-minute limit.
- Change one event timestamp and predict the prior event, elapsed time, outcome, and team rank before rerunning the query. Review Microsoft's window function references. LAG · OVER clause
Check your work
The report returns one row per ticket. Ticket 101 is late at 70 minutes, ticket 102 is within its limit at 20 minutes, and ticket 201 is within its limit at exactly 45 minutes. The learner can trace each result to its event rows and CTE stage.
Practice SQL
Open in SQL Flow;WITH EventLog AS (
SELECT TicketId, Team, Priority, SlaMinutes, EventId, EventAt, EventState, Payload
FROM (VALUES
(101, N'Apps', 2, 60, 1, CONVERT(datetime2(0), '2026-10-01T09:00:00'), N'Opened', N'{"owner":"Asha"}'),
(101, N'Apps', 2, 60, 2, CONVERT(datetime2(0), '2026-10-01T09:20:00'), N'Assigned', N'{"owner":"Asha"}'),
(101, N'Apps', 2, 60, 3, CONVERT(datetime2(0), '2026-10-01T10:10:00'), N'Resolved', N'{"owner":"Asha"}'),
(102, N'Apps', 1, 30, 4, CONVERT(datetime2(0), '2026-10-01T09:10:00'), N'Opened', N'{"owner":"Kabir"}'),
(102, N'Apps', 1, 30, 5, CONVERT(datetime2(0), '2026-10-01T09:30:00'), N'Resolved', N'{"owner":"Kabir"}'),
(201, N'Ops', 2, 45, 6, CONVERT(datetime2(0), '2026-10-01T08:00:00'), N'Opened', N'{"owner":"Mira"}'),
(201, N'Ops', 2, 45, 7, CONVERT(datetime2(0), '2026-10-01T08:45:00'), N'Resolved', N'{"owner":"Mira"}')
) AS v(TicketId, Team, Priority, SlaMinutes, EventId, EventAt, EventState, Payload)
), Annotated AS (
SELECT TicketId, Team, Priority, SlaMinutes, EventId, EventAt, EventState,
JSON_VALUE(Payload, '$.owner') AS OwnerName,
LAG(EventState) OVER (PARTITION BY TicketId ORDER BY EventAt, EventId) AS PreviousState,
DATEDIFF(minute, MIN(EventAt) OVER (PARTITION BY TicketId), EventAt) AS ElapsedMinutes
FROM EventLog
), TicketSummary AS (
SELECT TicketId, Team, Priority, SlaMinutes, MAX(OwnerName) AS OwnerName, COUNT(*) AS EventCount,
SUM(CASE WHEN PreviousState IS NOT NULL AND PreviousState <> EventState THEN 1 ELSE 0 END) AS StateTransitions,
MAX(CASE WHEN EventState = N'Resolved' THEN ElapsedMinutes END) AS ResolvedMinutes,
MAX(CASE WHEN EventState = N'Resolved' THEN EventAt END) AS ResolvedAt
FROM Annotated
GROUP BY TicketId, Team, Priority, SlaMinutes
), Classified AS (
SELECT TicketId, Team, Priority, SlaMinutes, OwnerName, EventCount, StateTransitions, ResolvedMinutes, ResolvedAt,
CASE WHEN ResolvedMinutes IS NULL THEN N'Open'
WHEN ResolvedMinutes > SlaMinutes THEN N'Late'
ELSE N'Within SLA' END AS Outcome
FROM TicketSummary
), Ranked AS (
SELECT *, RANK() OVER (PARTITION BY Team ORDER BY
CASE WHEN Outcome = N'Late' THEN 1 ELSE 0 END DESC,
ResolvedMinutes DESC) AS TeamRank
FROM Classified
)
SELECT TicketId, Team, Priority, OwnerName, EventCount, StateTransitions, ResolvedMinutes, SlaMinutes, Outcome, TeamRank
FROM Ranked
ORDER BY Team, TeamRank, TicketId;
Knowledge check
Commands in this lesson
Next lesson Capstone: reconcile a feed with a deduplicated MERGE