How to calculate 90th Percentile on time (either MM:SS or seconds )

Tamayo, Ashley 121 Reputation points
2020-09-01T21:21:00.607+00:00
SELECT DISTINCT

 ,DATEDIFF(s,MV_Incident.IncidentStartedDateTime,MV_Incident.IncidentDate) AS CalltakerProcessingTime ,DATEDIFF(s,MV_Incident.IncidentStartedDateTime,MV_Incident.FirstUnitDispatchedTime) AS TotalProcessingTime
 ,DATEDIFF(s,MV_Incident.IncidentDate,MV_Incident.FirstUnitDispatchedTime) AS GatekeeperProcessingTime

FROM
  MV_Incident

I am attempting to calculate the 90th percentile for time frames obtained from DATEDIFF. I am open to doing this in either the syntax of the query or in an expression in Report Builder. I know it is possible to do in excel but I am hopeful there is a way to achieve this automatically with a query or expression.

I am looking at a list of results like this:

03:11
02:09
01:35
00:57
02:36

90th percentile 02:57 (according to excel).

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,999 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 112.7K Reputation points MVP
    2020-09-01T21:43:27.557+00:00

    Here is an example of how you can do this. I remove the DISTINCT, because DISTINCT could have some interesting effects when computing percentiles.

    ; WITH CTE AS (
       SELECT DATEDIFF(s,MV_Incident.IncidentStartedDateTime,MV_Incident.IncidentDate)            AS CalltakerProcessingTime,
              DATEDIFF(s,MV_Incident.IncidentStartedDateTime,MV_Incident.FirstUnitDispatchedTime) AS TotalProcessingTime,
              DATEDIFF(s,MV_Incident.IncidentDate,MV_Incident.FirstUnitDispatchedTime)            AS GatekeeperProcessingTime
       FROM  MV_Incident
    ), percenttiles AS (
       SELECT percentile_cont(0.9) WITHIN GROUP (ORDER BY CalltakerProcessingTime)  OVER() AS CalltakerProcessingTime,
              percentile_cont(0.9) WITHIN GROUP (ORDER BY TotalProcessingTime)      OVER() AS TotalProcessingTime,
              percentile_cont(0.9) WITHIN GROUP (ORDER BY GatekeeperProcessingTime) OVER() AS GatekeeperProcessingTime
       FROM CTE
    )
    SELECT TOP (1) GatekeeperProcessingTime, TotalProcessingTime, GatekeeperProcessingTime
    FROM   percentiles
    

1 additional answer

Sort by: Most helpful
  1. MelissaMa-MSFT 24,201 Reputation points
    2020-09-02T06:32:41.163+00:00

    Hi anonymous user,

    You could use PERCENTILE_CONT in your situation.

    Please refer below example:

    CREATE TABLE #percentile   
    (  
    [TIME] DATETIME  
    )  
      
    INSERT INTO #percentile VALUES  
    ('2020-09-01 03:11:00'),  
    ('2020-09-01 02:09:00'),  
    ('2020-09-01 01:35:00'),  
    ('2020-09-01 00:57:00'),  
    ('2020-09-01 02:36:00')  
      
    ;WITH CTE AS (  
    SELECT top 1 P90= PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY DATEDIFF(s,(select MIN(TIME) from #percentile),time)) OVER () FROM #percentile)  
    select DATEADD(SECOND,P90,(select MIN(TIME) from #percentile )) P90 FROM CTE  
    

    Output:
    P90
    2020-09-01 02:57:00.000

    In addition, you could refer below query and check whether it helps.

    SELECT DISTINCT   
     percentile_cont(0.9) WITHIN GROUP (ORDER BY DATEDIFF(s,MV_Incident.IncidentStartedDateTime,MV_Incident.IncidentDate))  OVER() AS CalltakerProcessingTime,  
    percentile_cont(0.9) WITHIN GROUP (ORDER BY DATEDIFF(s,MV_Incident.IncidentStartedDateTime,MV_Incident.FirstUnitDispatchedTime))      OVER() AS TotalProcessingTime,  
    percentile_cont(0.9) WITHIN GROUP (ORDER BY DATEDIFF(s,MV_Incident.IncidentDate,MV_Incident.FirstUnitDispatchedTime)) OVER() AS GatekeeperProcessingTime  
    FROM  MV_Incident  
    

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.