Logo

SQL Server PERCENTRANKS Function

Updated 2023-06-15 22:08:29.247000

Description

Use the table-valued function PERCENTRANKS to calculate the relative standing of values within a dataset.

Syntax

SELECT * FROM [westclintech].[wct].[PERCENTRANKS](
  <@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 the ranks to be evaluated.

@Pmeth

The percent rank calculation method. Use 'INC' to return the equivalent of the Excel PERCENTRANK.INC function and 'EXC' for return the equivalent of the PERCENTRANK.EXC function.

Return Type

table

colNamecolDatatypecolDesc
Pfloatthe evaluated rank as supplied in @P.
Vfloata value between 0 and 1 representing the relative standing of the rank in the dataset.

Remarks

Any values in @P which are less than the minimum value or greater than the maximum value returned by @xQuery return a NULL.

If @P IS NULL, then PERCENTRANKS returns each DISTINCT value returned by @xQuery.

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

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

If @Pmeth is NULL, then @Pmeth = 'INC'.

Examples

In this example we return the rank for each value in the dataset using the 'INC' method.

SELECT
       *
FROM
       wct.PERCENTRANKS(
       'SELECT
              x
       FROM (VALUES
              (70),(70),(70),(80),(80),(90),(90),(100),(100),(100),(110),(110),(110),(120),(120)
              )n(x)
       ORDER BY 1',
       NULL,
       'INC');

This produces the following result.

PV
700
800.214285714285714
900.357142857142857
1000.5
1100.714285714285714
1200.928571428571429

Using the same data, we calculate the percent ranks using the 'EXC' method.

SELECT
       *
FROM
       wct.PERCENTRANKS(
       'SELECT
              x
       FROM (VALUES
              (70),(70),(70),(80),(80),(90),(90),(100),(100),(100),(110),(110),(110),(120),(120)
              )n(x)
       ORDER BY 1',
       NULL,
       'EXC');

This produces the following result.

PV
700.0625
800.25
900.375
1000.5
1100.6875
1200.875

In this example we use the SeriesFloat function to insert 100,000 randomly generated rows from a normal distribution having a mean of 100 and a standard deviation of 15 and we return all the percent ranks from 75 to 150 in increments of 1 using the 'INC' method. We will the use NMATRIX2STRING function from the XLeratorDB/math2008 library to create @P .

SELECT
       ROUND(SeriesValue,0) as X
INTO
       #g
FROM
       wct.SeriesFloat(100,15,NULL,100000,'N');
 
 
DECLARE @P as varchar(max) =(
       SELECT
              wct.NMATRIX2STRING(0,k.seq,k.seriesvalue)
       FROM
              wct.SeriesFloat(75,150,1,NULL,'L')k
       );
 
SELECT
       *
FROM
       wct.PERCENTRANKS(
              'SELECT x FROM #g ORDER BY x ASC',
              @P,
              NULL
              );

This produces the following result (your results will be different).

PV
750.045630456304563
760.052110521105211
770.0597705977059771
780.0680806808068081
790.0773507735077351
800.0875708757087571
810.0987109871098711
820.110731107311073
830.123271232712327
840.137531375313753
850.153431534315343
860.16940169401694
870.185941859418594
880.204562045620456
890.223622236222362
900.243372433724337
910.263812638126381
920.285712857128571
930.30780307803078
940.331873318733187
950.355853558535585
960.380373803738037
970.406174061740617
980.432194321943219
990.458774587745877
1000.486194861948619
1010.512845128451284
1020.53969539695397
1030.566015660156602
1040.592375923759238
1050.618506185061851
1060.643506435064351
1070.667676676766768
1080.691806918069181
1090.714377143771438
1100.737067370673707
1110.758377583775838
1120.77789777897779
1130.797077970779708
1140.815478154781548
1150.832928329283293
1160.849128491284913
1170.863308633086331
1180.87709877098771
1190.889258892588926
1200.902069020690207
1210.913169131691317
1220.92329923299233
1230.932709327093271
1240.940959409594096
1250.948149481494815
1260.9549995499955
1270.961239612396124
1280.96649966499665
1290.971089710897109
1300.975639756397564
1310.978769787697877
1320.982139821398214
1330.985139851398514
1340.987529875298753
1350.989339893398934
1360.991169911699117
1370.992609926099261
1380.993969939699397
1390.995019950199502
1400.99589995899959
1410.996619966199662
1420.997339973399734
1430.997869978699787
1440.998259982599826
1450.998579985799858
1460.998829988299883
1470.99909999099991
1480.999259992599926
1490.999449994499945
1500.999579995799958

In this example we insert 10,000 randomly generated rows from a normal distribution with a mean of 50 and a standard deviation of 2.5 into a table and calculate the percent ranks for all the members of the set using the 'EXC' method.

SELECT
       ROUND(SeriesValue,0) as X
INTO
       #g
FROM
       wct.SeriesFloat(50,2.5,NULL,10000,'N');
 
SELECT
       *
FROM
       wct.PERCENTRANKS(
              'SELECT X FROM #g ORDER BY 1',
              NULL,
              'EXC');
ORDER BY
       1;

This produces the following result (your results will be different).

PV
419.99900009999E-05
420.0003999600039996
430.0017998200179982
440.0042995700429957
450.0152984701529847
460.036996300369963
470.0826917308269173
480.163483651634837
490.28007199280072
500.422657734226577
510.58004199580042
520.726327367263274
530.842915708429157
540.917308269173083
550.965903409659034
560.988201179882012
570.996500349965003
580.999300069993001
590.999900009999

See Also

PERCENTILES - table-valued function to calculate one or many percentile values from a single dataset

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

PERCENTRANK_EXC - Calculate the rank of a value in dataset

PERCENTRANK_Q - Calculate the rank of a value in a dataset as a percentage of the dataset.