prestodb/sql

Merge a series of APPROX_PERCENTILE into a single ARRAY version

kaikalur opened this issue · 0 comments

Presto has two versions of APPROX_PERCENTILE - one that produces a single percentile and another that produces an array of APPROX_PERCENTILE values. So when you see something like:

SELECT
APPROX_PERCENTILE(x, 0.1) AS percentile_10,
APPROX_PERCENTILE(x, 0.2) AS percentile_20,
APPROX_PERCENTILE(x, 0.3) AS percentile_30
FROM T

Rewrite it it:

SELECT
percentiles[1] AS percentile_10,
percentiles[2] AS percentile_20,
percentiles[3] AS percentile_30
FROM (
SELECT APPROX_PERCENTILE(x, ARRAY[0.1, 0.2, 0.3]) AS percentiles
FROM T
)