Calculated field
WINDOW_PERCENTILE(SUM([Revenue]), 0.25)
Compute using: addressing Period; partitioning Region
Sample data
| Region | Period | Revenue | Cost |
|---|---|---|---|
| Harbor | 1 | 120 | 70 |
| Harbor | 2 | 180 | 100 |
| Harbor | 3 | 180 | 120 |
| Harbor | 4 | 240 | 140 |
| Upland | 1 | 90 | 50 |
| Upland | 2 | 150 | 110 |
| Upland | 3 | null | 90 |
| Upland | 4 | 330 | 190 |
The same calculation as SQL
SELECT CAST((quantile_cont("Revenue",0.25) OVER (PARTITION BY "Region" ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"Runs the example above.
SELECT CAST((PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY "Revenue") OVER (PARTITION BY "Region")) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"
Written for this dialect; not run here.
SELECT CAST((PERCENTILE_CONT(`Revenue`, 0.25) OVER (PARTITION BY `Region`)) AS FLOAT64) AS result FROM input ORDER BY `Region`, `Period`
Written for this dialect; not run here.
Result
| Region | Period | Revenue | Cost | result |
|---|---|---|---|---|
| Harbor | 1 | 120 | 70 | 165 |
| Harbor | 2 | 180 | 100 | 165 |
| Harbor | 3 | 180 | 120 | 165 |
| Harbor | 4 | 240 | 140 | 165 |
| Upland | 1 | 90 | 50 | 120 |
| Upland | 2 | 150 | 110 | 120 |
| Upland | 3 | null | 90 | 120 |
| Upland | 4 | 330 | 190 | 120 |
Same rows from the calculation and from the SQL in DuckDB.
Where a naive translation goes wrong
- Omitting the region partition allows a calculation to consume marks from other regions.
Naive SQL
SELECT CAST((quantile_cont("Revenue",0.25) OVER ( ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"Region Period Revenue Cost result Harbor 1 120 70 135 Harbor 2 180 100 135 Harbor 3 180 120 135 Harbor 4 240 140 135 Upland 1 90 50 135 Upland 2 150 110 135 Upland 3 null 90 135 Upland 4 330 190 135 Matches the calculation
SELECT CAST((quantile_cont("Revenue",0.25) OVER (PARTITION BY "Region" ORDER BY "Period" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) AS DOUBLE) AS result FROM input ORDER BY "Region", "Period"Region Period Revenue Cost result Harbor 1 120 70 165 Harbor 2 180 100 165 Harbor 3 180 120 165 Harbor 4 240 140 165 Upland 1 90 50 120 Upland 2 150 110 120 Upland 3 null 90 120 Upland 4 330 190 120
Signature
WINDOW_PERCENTILE(expression, percentile, [start, end])