Calculated field
PERCENTILE([amount], 0.25)
Sample data
| id | amount | units | score | other |
|---|---|---|---|---|
| 1 | -2.5 | 2 | -0.8 | 3 |
| 2 | 0 | 0 | 0 | 4 |
| 3 | 1.5 | 3 | 0.25 | 2 |
| 4 | 4 | 4 | 0.5 | 8 |
| 5 | 9 | 5 | 0.8 | 11 |
| 6 | 16 | 6 | 1 | 14 |
| 7 | null | 7 | null | 9 |
| 8 | 4 | 8 | -0.5 | null |
The same calculation as SQL
SELECT quantile_cont(amount,0.25) AS result FROM input
Runs the example above.
SELECT percentile_cont(0.25) WITHIN GROUP (ORDER BY amount) AS result FROM input
Written for this dialect; not run here.
SELECT DISTINCT percentile_cont(amount,0.25) OVER () AS result FROM input
Written for this dialect; not run here.
Result
| result |
|---|
| 0.75 |
Same rows from the calculation and from the SQL in DuckDB.
Where a naive translation goes wrong
- Replacing missing observations with zero changes the aggregate.
Naive SQL
SELECT quantile_cont(coalesce(amount,0),0.25) AS result FROM input
result 0 Matches the calculation
SELECT quantile_cont(amount,0.25) AS result FROM input
result 0.75
Signature
PERCENTILE(number, percentile)