PostgreSQL error code
PostgreSQL error 22014
22014 is the SQLSTATE PostgreSQL reports for invalid argument for ntile function, in class 22, Data Exception.
| SQLSTATE | 22014 |
|---|---|
| Condition name | invalid_argument_for_ntile_function |
| Class | 22 — Data Exception |
Reproduce it, then fix it
ntile(n) splits the rows into n groups, so n has to be at least 1. When the count is computed, guard it with greatest(n, 1). Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
SELECT name, price, ntile(0) OVER (ORDER BY price) AS quartile FROM products;
SELECT name, price, ntile(4) OVER (ORDER BY price) AS quartile FROM products;