I have the following code that queries the below data table that lists a couple Case IDs
and each has a number of time stamps associated with status changes. I need to pull First Contact Resolution
and Resolved
and then select the one with the most recent time stamp of the two. I attempted this with a CTE and using MAX()
but it's still providing CASE IDs
with both First Contact Resolution
and Resolved
statuses. Where am I going wrong?
WITH most_recent_status (Status, CaseId, TimeStamp) AS (
SELECT
svb_newvalue,
svb_caseid,
svb_timestamp
FROM Table1
WHERE svb_newvalue IN ('First Contact Resolution','Resolved')
)
SELECT
CaseId [Case ID],
MAX(TimeStamp) [Time Stamp],
Status
FROM
most_recent_status
GROUP BY CaseId, TimeStamp, Status
Data table:
Case ID | Time Stamp | Status |
---|---|---|
6045BD006793 | 7/27/23 15:19 | Resolved |
6045BD006793 | 7/26/23 17:32 | First Contact Resolution |
6045BD006793 | 7/27/23 15:17 | Active |
6045BD006239 | 7/18/23 17:46 | Pending Internal |
6045BD006239 | 7/18/23 17:57 | Routed |
6045BD006239 | 7/18/23 18:03 | First Contact Resolution |
6045BD006239 | 7/18/23 19:19 | Routed |
6045BD006239 | 7/18/23 16:36 | Resolved |
6045BD006239 | 7/18/23 16:05 | Routed |
The Resulting table:
Case ID | Time Stamp | Status |
---|---|---|
6045BD006793 | 7/27/23 15:19 | Resolved |
6045BD006793 | 7/26/23 17:32 | First Contact Resolution |
6045BD006239 | 7/18/23 18:03 | First Contact Resolution |
6045BD006239 | 7/18/23 16:36 | Resolved |
What should happen:
Case ID | Time Stamp | Status |
---|---|---|
6045BD006793 | 7/27/23 15:19 | Resolved |
6045BD006239 | 7/18/23 18:03 | First Contact Resolution |
MAX()
on theTimeStamp
column, they'll get the row with theStatus
that matches thatTimeStamp
. That's probably their misconception here. If so, then there's a few easy solutions.