Fix “Binder Error: APPROXIMATE QUANTILE can only take parameters in range [0, 1]” in DuckDB

If you’re getting an error that reads “Binder Error: APPROXIMATE QUANTILE can only take parameters in range [0, 1]” in DuckDB, it appears that you’re passing an invalid second argument to the approx_quantile() function.

To fix this issue, make sure the second argument is between 0 and 1.

Example of Error

Suppose we create and populate the following table:

CREATE TABLE sales (
    sale_id INTEGER,
    sale_amount DOUBLE
);

INSERT INTO sales (sale_id, sale_amount)
VALUES
    (1, 100.0),
    (2, 200.0),
    (3, 300.0),
    (4, 400.0),
    (5, 500.0),
    (6, 600.0),
    (7, 700.0),
    (8, 800.0),
    (9, 900.0),
    (10, 1000.0);

Here’s an example of code that produces the error:

SELECT 
    approx_quantile(sale_amount, 2)
FROM sales;

Output:

Binder Error: APPROXIMATE QUANTILE can only take parameters in range [0, 1]

We got an error, because the second argument is greater than 1.

We’ll get the same error if we use a negative value:

SELECT 
    approx_quantile(sale_amount, -1)
FROM sales;

Output:

Binder Error: APPROXIMATE QUANTILE can only take parameters in range [0, 1]

Solution

To fix this issue, we must make sure that the second argument is between 0 and 1:

SELECT 
    approx_quantile(sale_amount, 0.5)
FROM sales;

Result:

+-----------------------------------+
| approx_quantile(sale_amount, 0.5) |
+-----------------------------------+
| 550.0 |
+-----------------------------------+

This time it ran without error.

The second argument is for the quantile that we want to use, and so any value outside 0 and 1 doesn’t make sense. The same concept applies to the reservoir_quantile() function.