PostgreSQL error code
PostgreSQL error 22013
22013 is the SQLSTATE PostgreSQL reports for invalid preceding or following size, in class 22, Data Exception.
| SQLSTATE | 22013 |
|---|---|
| Condition name | invalid_preceding_or_following_size |
| Class | 22 — Data Exception |
Reproduce it, then fix it
A frame offset counts rows back or forward from the current one, so it can’t be negative. For a window over the previous row and this one, write 1 PRECEDING. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
SELECT id, price, avg(price) OVER (ORDER BY id ROWS BETWEEN -1 PRECEDING AND CURRENT ROW) AS moving_avg FROM products;
SELECT id, price, avg(price) OVER (ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS moving_avg FROM products;