Logo

SQL Server PERCENTILES Function

Updated 2023-10-31 14:51:16.883000

Description

Use the table-valued function PERCENTILES to calculate any number of percentiles from a set of values.

Syntax

SELECT * FROM [westclintech].[wct].[PERCENTILES](
  <@xQuery, nvarchar(max),>
 ,<@P, nvarchar(max),>
 ,<@Pmeth, nvarchar(4000),>)

Arguments

@xQuery

A SQL statement which returns a resultant table consisting of a single column of numeric data sorted in ascending order.

@P

A comma separated list of values between 0 and 1 from which the percentile values are calculated.

@Pmeth

The percentile calculation method. Use 'INC' to return the equivalent of the Excel PERCENTILE.INC function and 'EXC' for return the equivalent of the PERCENTILE.EXC function.

Return Type

table

colNamecolDatatypecolDesc
Pfloatthe percentile as supplied in @P.
Vfloatthe Pth percentile value

Remarks

Any values less than zero or greater than 1 in @P are ignored.

If @P IS NULL, then PERCENTILES generates values from 0 to 1 in increments of .01.

Percentile values are interpolated when the requested percentile lies between 2 values in the ordered resultant table.

If the results of @xQuery are not in ascending order, then the percentiles returned by the function will be unreliable.

Examples

In this example we will select the 25 th , 50 th , 75 th and 100 th percentiles of the ordered list {3,6,7,8,8,10,13,15,16,20} using the 'INC' method.

SELECT *
FROM wct.PERCENTILES(
                        'SELECT x FROM (VALUES (3),(7),(8),(10),(13),(15),(8),(16)
                                  ,(20),(6))n(x) ORDER BY 1',
                        '0.25,0.50,0.75,1',
                        'INC'
                    );

This produces the following result.

PV
0.257.25
0.59
0.7514.5
120

In this example we use the SeriesFloat function to insert 100,000 rows from a normal distribution having a mean of 100 and a standard deviation of 15 and we return all the percentiles from 0 to 1 in increments of .01 using the 'INC' method.

SELECT ROUND(SeriesValue, 0) as X
INTO #g
FROM wct.SeriesFloat(100, 15, NULL, 100000, 'N');
SELECT *
FROM wct.PERCENTILES('SELECT X FROM #g ORDER BY 1', NULL, NULL)
ORDER BY 1;
DROP TABLE #g;

This produces the following result.

PV
041
0.0165
0.0269
0.0372
0.0474
0.0575
0.0677
0.0778
0.0879
0.0980
0.181
0.1182
0.1282
0.1383
0.1484
0.1584
0.1685
0.1786
0.1886
0.1987
0.287
0.2188
0.2288
0.2389
0.2489
0.2590
0.2690
0.2791
0.2891
0.2992
0.392
0.3193
0.3293
0.3393
0.3494
0.3594
0.3695
0.3795
0.3895
0.3996
0.496
0.4196
0.4297
0.4397
0.4498
0.4598
0.4698
0.4799
0.4899
0.4999
0.5100
0.51100
0.52101
0.53101
0.54101
0.55102
0.56102
0.57103
0.58103
0.59103
0.6104
0.61104
0.62104
0.63105
0.64105
0.65106
0.66106
0.67106
0.68107
0.69107
0.7108
0.71108
0.72109
0.73109
0.74109
0.75110
0.76110
0.77111
0.78111
0.79112
0.8113
0.81113
0.82114
0.83114
0.84115
0.85115
0.86116
0.87117
0.88117
0.89118
0.9119
0.91120
0.92121
0.93122
0.94123
0.95124
0.96126
0.97128
0.98131
0.99135
1163

In this example we insert 10,000 rows from a normal distribution with a mean of 50 and a standard deviation of 25 into a table and calculate the percentiles from 0 to 1 in increments of .01 using the 'EXC' method.

SELECT ROUND(SeriesValue, 1) as X
INTO #g
FROM wct.SeriesFloat(50, 25, NULL, 10000, 'N');
SELECT *
FROM wct.PERCENTILES('SELECT X FROM #g ORDER BY 1', NULL, 'EXC')
ORDER BY 1;
DROP TABLE #g;

This produces the following result.

PV
0NULL
0.01-8.599
0.02-2.298
0.032.203
0.045.7
0.058.2
0.0610.7
0.0712.7
0.0814.6
0.0916
0.117.2
0.1118.7
0.1220
0.1321.3
0.1422.4
0.1523.5
0.1624.616
0.1725.7
0.1826.7
0.1927.6
0.228.4
0.2129.3
0.2230.2
0.2330.923
0.2431.8
0.2532.6
0.2633.3
0.2734.1
0.2834.9
0.2935.7
0.336.5
0.3137.2
0.3237.9
0.3338.7
0.3439.334
0.3540
0.3640.8
0.3741.4
0.3842
0.3942.6
0.443.3
0.4144
0.4244.7
0.4345.4
0.4446.144
0.4546.8
0.4647.446
0.4748
0.4848.6
0.4949.2
0.549.8
0.5150.4
0.5251.1
0.5351.7
0.5452.3
0.5552.8
0.5653.4
0.5753.9
0.5854.6
0.5955.2
0.655.9599999999999
0.6156.6
0.6257.3
0.6358.063
0.6458.7
0.6559.5
0.6660.3
0.6760.8
0.6861.5
0.6962.1
0.762.7
0.7163.6
0.7264.3
0.7365
0.7465.8
0.7566.7
0.7667.6
0.7768.5
0.7869.3780000000001
0.7970.2
0.871
0.8171.9
0.8273
0.8374.1
0.8475.3
0.8576.1
0.8677.2860000000001
0.8778.4870000000001
0.8879.8879999999999
0.8981.1
0.982.7
0.9184.091
0.9285.592
0.9387.2
0.9489
0.9591.5
0.9694.2
0.9797.8
0.98102.298
0.99108.4
1NULL

In this example we calculate the 64th percentile from the ordered set {15,20,35,40,50} using the 'EXC' method.

SELECT *
FROM wct.PERCENTILES('SELECT x FROM (VALUES (15),(20),(35),(40),(50))n(x) ORDER 
          BY 1', 0.64, 'EXC');

This produces the following result.

PV
0.6439.2

See Also

PERCENTILE - Aggregate function to calculate a percentile using the 'INC' method

PERCENTILE_EXC - Calculate percentile.

PERCENTILE_Q - Calculate percentile.

PERCENTRANK - Aggregate function to calculate a percent rank using the 'INC' method

PERCENTRANK_EXC - Calculate the rank of a value in dataset