1

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
8
  • 1
    If you GROUP BY Case ID, Timestamp and Status, then you're inherently telling SQL to treat rows with different values in those fields as separate rows (and therefore assess any aggregate, like MAX(), on those individual rows). Therefore, the rows that you are trying to aggregate/collapse, will never aggregate
    – Craig
    Commented Aug 13, 2023 at 23:51
  • 2
    Say that you happened to have two rows, for the same Case ID, with the same Timestamp - but one row is "Resolved" and one row is "First Contact Resolution" - which status would you expect to see, in that instance?
    – Craig
    Commented Aug 13, 2023 at 23:53
  • 1
    @Craig Very valid question. I'm assuming OP is thinking because they're using MAX() on the TimeStamp column, they'll get the row with the Status that matches that TimeStamp. That's probably their misconception here. If so, then there's a few easy solutions.
    – J.D.
    Commented Aug 14, 2023 at 5:08
  • 1
    @hobosapien I'll try to post and answer to explain when I get a chance (or maybe someone can post one before me). My go-to for a problem where you want the latest row of a grouping is using something called a window function. I'll elaborate in my answer.
    – J.D.
    Commented Aug 14, 2023 at 19:18
  • 1
    @hobosapien - in my experience, the one thing that you think CAN'T happen in your data is invariably the one thing that WILL happen (and usually when it's least needed!)
    – Craig
    Commented Aug 14, 2023 at 21:50

1 Answer 1

1

You can use this script. Keep in mind that Craig has a good point and you might want to add that to the query.

WITH cte
AS (SELECT
             t.svb_caseid,
             MAX(t.svb_timestamp) AS LastTimeStamp
    FROM     @Table1 AS t
    WHERE    t.svb_newvalue IN ( 'First Contact Resolution', 'Resolved' )
    GROUP BY t.svb_caseid)
SELECT
     a.svb_caseid AS [Case ID],
     a.svb_timestamp AS [Time Stamp],
     a.svb_newvalue AS [Status]
FROM cte AS c
     CROSS APPLY (
                    SELECT   TOP (1)
                             t.svb_caseid,
                             t.svb_timestamp,
                             t.svb_newvalue
                    FROM     @Table1 AS t
                    WHERE    t.svb_caseid = c.svb_caseid
                             AND t.svb_timestamp = c.LastTimeStamp
                             AND t.svb_newvalue IN ( 'First Contact Resolution', 'Resolved' )
                    ORDER BY t.svb_newvalue ASC
                 /*Add logic to see which is more important*/
                 ) a;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.