Skip to main content

Categories: Percentile

PERCENTILE_CONT

Computes a percentile based on a continuous distribution of the column values, interpolating between adjacent values if needed.

Syntax​

PERCENTILE_CONT(fraction numeric) WITHIN GROUP ( ORDER BY order_by_expression numeric [ ASC | DESC ] ) → double precision​

  • fraction: The fraction/percentile value to compute. The value for this must be a numeric literal in the range of 0 to 1 inclusive and represents a percentage.
  • order_by_expression: The expression to sort and compute the percentile. You can only provide one expression in the ORDER BY clause. By default, the sort order is ascending (ASC).

Examples

PERCENTILE_CONT example
SELECT PERCENTILE_CONT(0.6) WITHIN GROUP ( ORDER BY pop ASC ),
PERCENTILE_CONT(0.6) WITHIN GROUP ( ORDER BY pop DESC )
FROM Samples."samples.dremio.com"."zips.json"
-- EXPR$0, EXPR$1
-- 4519.2, 1806.0
Calculate the 50th percentile
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY v) FROM (VALUES (1), (2), (3), (4), (5)) AS t(v)
-- 3.0
Calculate an interpolated 25th percentile
SELECT PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY v) FROM (VALUES (10), (20), (30), (40)) AS t(v)
-- 17.5
Calculate the 0th percentile
SELECT PERCENTILE_CONT(0.0) WITHIN GROUP (ORDER BY v) FROM (VALUES (1), (2), (3)) AS t(v)
-- 1.0
Calculate the 100th percentile
SELECT PERCENTILE_CONT(1.0) WITHIN GROUP (ORDER BY v) FROM (VALUES (1), (2), (3)) AS t(v)
-- 3.0

Usage Notes​

This function computes a percentile based on a continuous distribution of the column value. The result is interpolated and may not be equal to any of the actual values in the column. This information was originally provided via Microsoft's Transact-SQL Reference Guide.

This function is used with only numeric data types in Dremio. NULL values in the dataset are ignored. However, passing NULL as fraction or as order_by_expression will cause an error.