Query

  • Hi ,

    Please help in writing the query to get the below result i have a Requested details table which contains the data in the below formate.

    PkeyParentIdStatusDatelogged
    1123Submitted12/9/2018 10:30
    2123Assigned12/9/2018 10:40
    3123Acknowledge12/9/2018 10:45
    4123OnHold12/9/2018 10:55
    5123Resloved12/9/2018 11:15
    6124Submitted12/9/2018 10:30
    7124Assigned12/9/2018 10:40
    8124Acknowledge12/9/2018 10:45
    9124OnHold12/9/2018 10:55
    10124Work In Progress12/9/2018 11:16
    11124Resloved12/9/2018 11:45

    I want the result in the like below.

  • ParentIdTotal Resolved  time (min)Actual Resloved Time (min)
    1234525
    1247554

  •  

    total resolved time is the the total time take to resolve the ticket time difference resolved - Submitted . e.g for 123 request 12/9/2018 11:15-12/9/2018 10:30 = 45 mins
    Actual resolved time is the Total resolve time - request on hold time.
    e.g
    for 123 request the total resolve time is 45 mins. , but the request is moved to on hold state 12/9/2018 10:55:00 AM and at 11:15 is moved directly to resolved state. so i have we have subtract 20 mins for the total resolve time . so the actual resolved time is 45-20=25 mins

    For 124 request total resolved time is 75 min, the request is moved to on hold state at 12/9/2018 10:55:00 AM and then at 12/9/2018 11:16:00 AM the request is moved to work in progress state. we have subtract the 21 mins from the total resolve time. i.e. 75-21=54

  • This uses a pivot table...


    USE [tempdb];

    CREATE TABLE dbo.Requested
    (Pkey Int, ParentId Int, [Status] Varchar(50), Datelogged DateTime);

    SET DATEFORMAT DMY; -- To get British dates

    INSERT INTO dbo.Requested
    VALUES
    (1,    123,    'Submitted',    '12/9/2018 10:30'),
    (2,    123,    'Assigned',    '12/9/2018 10:40'),
    (3,    123,    'Acknowledge',    '12/9/2018 10:45'),
    (4,    123,    'OnHold',    '12/9/2018 10:55'),
    (5,    123,    'Resolved',    '12/9/2018 11:15'),
    (6,    124,    'Submitted',    '12/9/2018 10:30'),
    (7,    124,    'Assigned',    '12/9/2018 10:40'),
    (8,    124,    'Acknowledge',    '12/9/2018 10:45'),
    (9,    124,    'OnHold',    '12/9/2018 10:55'),
    (10,    124,    'Work In Progress',    '12/9/2018 11:16'),
    (11,    124,    'Resolved',    '12/9/2018 11:45');

    WITH CTE AS
    (
    SELECT ParentId, 
    [Submitted], [Assigned], [Acknowledge], [OnHold], [Work In Progress], [Resolved]
    FROM
    (SELECT ParentId, [Status], Datelogged 
      FROM dbo.Requested) AS SourceTable
    PIVOT
    (
    MAX(Datelogged)
    FOR [Status] IN ([Submitted], [Assigned], [Acknowledge], [OnHold], [Work In Progress], [Resolved])
    ) AS PivotTable
    )
    SELECT ParentId, 
        [Submitted], [Assigned], [Acknowledge], [OnHold], [Work In Progress], [Resolved],
        [TotalResolvedTime] = DATEDIFF(mi,[Submitted], [Resolved]),
        [ActualResolvedTime] = DATEDIFF(mi,[Submitted], [Resolved]) - CASE WHEN [Work In Progress] IS NULL THEN DATEDIFF(mi,[OnHold], [Resolved]) ELSE DATEDIFF(mi,[OnHold], [Work In Progress]) END
    FROM CTE;

  • You can use LEAD to get the time between OnHold and the next status

    CREATE TABLE #temp
    (
    [Pkey] INT,
    [ParentId] INT,
    [Status] VARCHAR(40),
    [Datelogged] DATETIME
    )

    INSERT INTO #temp
    VALUES
    (1,123,'Submitted','12/9/2018 10:30'),
    (2,123,'Assigned','12/9/2018 10:40'),
    (3,123,'Acknowledge','12/9/2018 10:45'),
    (4,123,'OnHold','12/9/2018 10:55'),
    (5,123,'Resolved','12/9/2018 11:15'),
    (6,124,'Submitted','12/9/2018 10:30'),
    (7,124,'Assigned','12/9/2018 10:40'),
    (8,124,'Acknowledge','12/9/2018 10:45'),
    (9,124,'OnHold','12/9/2018 10:55'),
    (10,124,'WorkInProgress','12/9/2018 11:16'),
    (11,124,'Resolved','12/9/2018 11:45');

    WITH TimeInStateCTE AS
    (
      SELECT
       [ParentId],
       [Status],
       DATEDIFF(MINUTE, [Datelogged],LEAD([Datelogged],1) OVER (PARTITION BY [ParentId] ORDER BY [Datelogged])) AS [timeInState]
      FROM #temp
    ),Final AS
    (
      SELECT
       #temp.[ParentId],
       DATEDIFF(MINUTE, MIN(Datelogged),MAX(Datelogged)) AS [Total Resolved time (min)],
       DATEDIFF(MINUTE, MIN(Datelogged),MAX(Datelogged)) - tis.timeInState AS [Actual Resolved time (min)]
      FROM #temp
      CROSS APPLY (SELECT [ParentId], [timeInState] FROM TimeInStateCTE WHERE [Status] = 'OnHold') tis
       WHERE #temp.[ParentId] = tis.[ParentId]
      GROUP BY #temp.[ParentId], tis.timeInState
    )
    SELECT * FROM Final

    DROP TABLE #temp

  • Try this on for size and performance:
    CREATE TABLE #Requested (
        Pkey Int,
        ParentId Int,
        [Status] Varchar(50),
        Datelogged DateTime
    );
    INSERT INTO #Requested
        (
        Pkey,
        ParentId,
        [Status],
        Datelogged
        )
        VALUES    (1,  123,  'Submitted',  '12/9/2018 10:30'),
                (2,  123,  'Assigned',  '12/9/2018 10:40'),
                (3,  123,  'Acknowledge',  '12/9/2018 10:45'),
                (4,  123,  'OnHold',  '12/9/2018 10:55'),
                (5,  123,  'Resolved',  '12/9/2018 11:15'),
                (6,  124,  'Submitted',  '12/9/2018 10:30'),
                (7,  124,  'Assigned',  '12/9/2018 10:40'),
                (8,  124,  'Acknowledge',  '12/9/2018 10:45'),
                (9,  124,  'OnHold',  '12/9/2018 10:55'),
                (10, 124,  'Work In Progress',  '12/9/2018 11:16'),
                (11, 124,  'Resolved',  '12/9/2018 11:45');

    WITH ELIGIBLE_CASES AS (

        SELECT R.*,
            CASE
                WHEN R.[Status] = 'Submitted' THEN R.Datelogged
                ELSE NULL
            END AS Submitted,
            CASE
                WHEN R.[Status] = 'Resolved' THEN R.Datelogged
                ELSE NULL
            END AS ResolvedDateLogged,
            DATEDIFF(minute, H.OnHoldDateLogged, LEAD(R.DateLogged, 1) OVER(PARTITION BY R.ParentId ORDER BY R.Datelogged)) AS OnHoldTime
        FROM #Requested AS R
        OUTER APPLY (
            SELECT
                CASE
                    WHEN R.[Status] = 'OnHold' THEN R.Datelogged
                    ELSE NULL
                END AS OnHoldDateLogged
            ) AS H
        WHERE EXISTS (
            SELECT 1
            FROM #Requested AS R2
            WHERE    R2.ParentId = R.ParentId
                AND R2.[Status] = 'Resolved'
            )
    )
    SELECT
        EC.ParentId,
        DATEDIFF(minute, MAX(EC.Submitted), MAX(EC.ResolvedDateLogged)) AS TotalResolutionTime,
        DATEDIFF(minute, MAX(EC.Submitted), MAX(EC.ResolvedDateLogged)) -
            SUM(EC.OnHoldTime)                                            AS ActualResolutionTime
    FROM ELIGIBLE_CASES AS EC
    GROUP BY EC.ParentId
    ORDER BY EC.ParentId;

    DROP TABLE #Requested;

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • Revising my previous version
    CREATE TABLE #temp
    (
    [Pkey] INT,
    [ParentId] INT,
    [Status] VARCHAR(40),
    [Datelogged] DATETIME
    )

    INSERT INTO #temp
    VALUES
    (1,123,'Submitted','12/9/2018 10:30'),
    (2,123,'Assigned','12/9/2018 10:40'),
    (3,123,'Acknowledge','12/9/2018 10:45'),
    (4,123,'OnHold','12/9/2018 10:55'),
    (5,123,'Resolved','12/9/2018 11:15'),
    (6,124,'Submitted','12/9/2018 10:30'),
    (7,124,'Assigned','12/9/2018 10:40'),
    (8,124,'Acknowledge','12/9/2018 10:45'),
    (9,124,'OnHold','12/9/2018 10:55'),
    (10,124,'WorkInProgress','12/9/2018 11:16'),
    (11,124,'Resolved','12/9/2018 11:45');

    WITH Final AS
    (
      SELECT
       [ParentId],
       [Status],
       DATEDIFF(MINUTE, [Datelogged],LEAD([Datelogged],1) OVER (PARTITION BY [ParentId] ORDER BY [Datelogged])) AS [timeInState]
    FROM #temp
    )
    SELECT
      [ParentId],
      SUM([timeInState]) AS [Total Resolved time (min)],
      SUM(CASE WHEN [status] ='onHold' THEN 0 ELSE [timeInState] end) AS [Actual Resolved time (min)]
    FROM Final
    GROUP BY [ParentId]

    DROP TABLE #temp...

  • Viewing 5 posts - 1 through 4 (of 4 total)

    You must be logged in to reply to this topic. Login to reply