3267
MariaDBERRORNotableWindow FunctionsHIGH confidence

RANGE frame with numeric offset requires numeric ORDER BY

Production Risk

Low — query is rejected; correct ORDER BY type or frame type.

How to reproduce
trigger — this will error
trigger — this will error
SELECT SUM(amount) OVER (ORDER BY name RANGE BETWEEN 5 PRECEDING AND CURRENT ROW) FROM sales;

expected output

ERROR 3267 (HY000): Window 'w': RANGE with offset PRECEDING/FOLLOWING requires numeric ORDER BY.

Fix 1

Use ROWS frame

Use ROWS frame
SELECT SUM(amount) OVER (ORDER BY name ROWS BETWEEN 5 PRECEDING AND CURRENT ROW) FROM sales;

Why this works

ROWS frames work with any ORDER BY type.

Fix 2

Order by a numeric column

Order by a numeric column
SELECT SUM(amount) OVER (ORDER BY sale_id RANGE BETWEEN 5 PRECEDING AND CURRENT ROW) FROM sales;

Why this works

Numeric ORDER BY is required for numeric RANGE offsets.

What not to do

Sources
Official documentation ↗

MySQL 8.0 — 3267 ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE2

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

← All MariaDB errors