The PERCENTILE_DISC (discrete percentile) function is an inverse distribution function that assumes a discrete distribution model. It takes a percentile value and a sort specification and returns the value from the set that corresponds to that percentile. Unlike PERCENTILE_CONT, which interpolates between values to find a continuous result, PERCENTILE_DISC always returns an actual value from the input set.
In this scenario, we have a set of four values: $\{1, 2, 3, 4\}$. The query specifies a descending order (order by v desc), so the ordered set for the calculation is $\{4, 3, 2, 1\}$.
To find the discrete percentile, Snowflake calculates the cumulative distribution. For a set of $N$ elements, each element represents a percentile rank of $1/N$. With 4 elements, each covers 25% ($0.25$) of the distribution:
Value 4: Cumulative Percentile $0.25$
Value 3: Cumulative Percentile $0.50$
Value 2: Cumulative Percentile $0.75$
Value 1: Cumulative Percentile $1.00$
The PERCENTILE_DISC(0.60) function looks for the first value whose cumulative distribution is greater than or equal to the specified percentile ($0.60$).
$0.25$ (Value 4) is not $\ge 0.60$.
$0.50$ (Value 3) is not $\ge 0.60$.
$0.75$ (Value 2) is the first value where the cumulative distribution is $\ge 0.60$.
Therefore, the result is 2. If the order had been ascending (ASC), the cumulative distribution would have been $\{1: 0.25, 2: 0.50, 3: 0.75, 4: 1.00\}$, and the result for $0.60$ would have been 3. Understanding the impact of the ORDER BY clause within the WITHIN GROUP syntax is a critical skill for the Data Analysis domain of the SnowPro Advanced: Data Analyst exam.
Submit