MySQL throws ER_WINDOW_DUPLICATE_NAME when the WINDOW clause in a SELECT statement defines the same window name more than once. Each named window specification must have a unique identifier to avoid ambiguity when functions reference them.
When using named windows in MySQL, you define a reusable window specification in the WINDOW clause with a unique name. Each name must appear exactly once in the WINDOW clause. If you use the same window name twice in the WINDOW clause, MySQL raises ER_WINDOW_DUPLICATE_NAME because the parser cannot determine which definition to use when the window is referenced by name in an OVER clause. The error occurs at parse time, before the query executes. This is different from reusing the same window name across multiple OVER clauses (which is allowed) or modifying a named window with additional clauses in the OVER clause (which is also allowed). The restriction is specifically about duplicate definitions in the WINDOW clause itself. ```sql -- Error: window 'w' is defined twice SELECT id, SUM(value) OVER w AS sum1, AVG(value) OVER w AS sum2 FROM metrics WINDOW w AS (PARTITION BY account_id ORDER BY created_at), w AS (ORDER BY created_at); -- ERROR 3591: duplicate name 'w' -- Fixed: each window has a unique name SELECT id, SUM(value) OVER w1 AS sum1, AVG(value) OVER w2 AS sum2 FROM metrics WINDOW w1 AS (PARTITION BY account_id ORDER BY created_at), w2 AS (ORDER BY created_at); ```
Scan your WINDOW clause for all window names. Example:
SELECT col1, SUM(col2) OVER w1, AVG(col2) OVER w2
FROM table_name
WINDOW w1 AS (PARTITION BY col1 ORDER BY col2),
w2 AS (PARTITION BY col1 ORDER BY col3);Check that each name (w1, w2, etc.) appears exactly once. If you see a name listed more than once, that is the problem.
Choose a descriptive new name for the duplicate window and update both the WINDOW clause definition and any OVER clauses that reference it.
-- Before: 'w' is defined twice
SELECT col1, SUM(col2) OVER w, AVG(col2) OVER w
FROM table_name
WINDOW w AS (PARTITION BY col1 ORDER BY col2),
w AS (ORDER BY col3);
-- After: second 'w' renamed to 'w_unordered'
SELECT col1, SUM(col2) OVER w, AVG(col2) OVER w_unordered
FROM table_name
WINDOW w AS (PARTITION BY col1 ORDER BY col2),
w_unordered AS (ORDER BY col3);Make sure the new name is used consistently in all OVER clauses that reference it.
If both window definitions are identical (same PARTITION BY, ORDER BY, and frame specification), you only need one. Delete the duplicate and use the single window name in all relevant OVER clauses.
-- Before: 'w' is defined twice with identical specs
SELECT col1, SUM(col2) OVER w, AVG(col2) OVER w
FROM table_name
WINDOW w AS (PARTITION BY col1 ORDER BY col2),
w AS (PARTITION BY col1 ORDER BY col2);
-- After: keep only one definition
SELECT col1, SUM(col2) OVER w, AVG(col2) OVER w
FROM table_name
WINDOW w AS (PARTITION BY col1 ORDER BY col2);This simplifies the query and eliminates the ambiguity.
After renaming or removing duplicates, double-check that every OVER clause that references a named window uses the correct (non-duplicate) name.
SELECT
ROW_NUMBER() OVER w1 AS rn,
SUM(amount) OVER w2 AS running_total
FROM sales
WINDOW w1 AS (ORDER BY sale_date),
w2 AS (PARTITION BY customer_id ORDER BY sale_date);If a name in an OVER clause does not match any name in the WINDOW clause, you will get ER_WINDOW_NO_SUCH_WINDOW instead. Ensure all references are consistent.
MySQL allows you to reuse the same window name across multiple OVER clauses and even to inherit from one named window to another (for example, WINDOW w2 AS (w1 ORDER BY col) inherits w1's definition and adds ordering). The uniqueness constraint applies only to the definitions in the WINDOW clause itself, not to their usage. If you need multiple similar windows with subtle differences (different partitioning or ordering), give each a distinct name rather than trying to redefine the same one.
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ERROR 1060: Duplicate column name
How to fix "ERROR 1060: Duplicate column name" in MySQL