22014
PostgreSQLERRORNotableData ExceptionHIGH confidence

invalid argument for ntile function

What this means

SQLSTATE 22014 is raised when the NTILE window function receives an argument that is not a positive integer — for example, zero, a negative number, or NULL.

Why it happens
  1. 1Calling NTILE(0) or NTILE with a negative or NULL argument
  2. 2A dynamic expression for the NTILE bucket count evaluates to a non-positive value
How to reproduce

NTILE with zero buckets.

trigger — this will error
trigger — this will error
SELECT NTILE(0) OVER (ORDER BY salary) FROM employees;

expected output

ERROR:  argument of ntile must be greater than zero

Fix

Ensure the NTILE argument is a positive integer

WHEN When the bucket count is computed dynamically.

Ensure the NTILE argument is a positive integer
SELECT NTILE(GREATEST(bucket_count, 1)) OVER (ORDER BY salary)
FROM employees;

Why this works

GREATEST ensures the bucket count is at least 1, preventing the 22014 error.

Sources
Official documentation ↗

Class 22 — Data Exception

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors