PostgreSQL error code
PostgreSQL error 22016
22016 is the SQLSTATE PostgreSQL reports for invalid argument for nth value function, in class 22, Data Exception.
| SQLSTATE | 22016 |
|---|---|
| Condition name | invalid_argument_for_nth_value_function |
| Class | 22 — Data Exception |
Reproduce it, then fix it
nth_value counts from 1, so the first row of the frame is nth_value(x, 1). There is no zeroth row. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
SELECT DISTINCT nth_value(name, 0) OVER ( ORDER BY price DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS second_priciest FROM products;
SELECT DISTINCT nth_value(name, 2) OVER ( ORDER BY price DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS second_priciest FROM products;