DDC-2836: DQL errors when attempting to use GROUP BY MAX(field) #2981

Closed
opened 2026-01-22 14:08:56 +01:00 by admin · 6 comments
Owner

Originally created by @doctrinebot on GitHub (Mar 26, 2013).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user intel352:

Attempting to run DQL similar to:
SELECT a FROM ClassName a GROUP BY MAX(a.depth)

Throws error:
[Semantical Error] line 0, col 250 near 'MAX(ao.depth)': Error: Cannot group by undefined identification or result variable.

Per docs, MAX is allowed within GROUP BY: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#aggregate-functions

If this error is due to some omission on my part, then perhaps the docs should be extended to show a valid GROUP BY MAX() usage, or the error message expanded for a better hint.

Originally created by @doctrinebot on GitHub (Mar 26, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user intel352: Attempting to run DQL similar to: SELECT a FROM ClassName a GROUP BY MAX(a.depth) Throws error: [Semantical Error] line 0, col 250 near 'MAX(ao.depth)': Error: Cannot group by undefined identification or result variable. Per docs, MAX is allowed within GROUP BY: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#aggregate-functions If this error is due to some omission on my part, then perhaps the docs should be extended to show a valid GROUP BY MAX() usage, or the error message expanded for a better hint.
admin added the Bug label 2026-01-22 14:08:56 +01:00
admin closed this issue 2026-01-22 14:08:57 +01:00
Author
Owner

@doctrinebot commented on GitHub (Dec 3, 2013):

Comment created by @ocramius:

Grouping by MAX() is not supported, as MAX() is already an aggregation

@doctrinebot commented on GitHub (Dec 3, 2013): Comment created by @ocramius: Grouping by MAX() is not supported, as MAX() is already an aggregation
Author
Owner

@doctrinebot commented on GitHub (Dec 3, 2013):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Dec 3, 2013): Issue was closed with resolution "Invalid"
Author
Owner

@doctrinebot commented on GitHub (Dec 3, 2013):

Comment created by intel352:

Shame, because that is useful functionality in SQL. So I assume this means the documentation will be correct then?

@doctrinebot commented on GitHub (Dec 3, 2013): Comment created by intel352: Shame, because that is useful functionality in SQL. So I assume this means the documentation will be correct then?
Author
Owner

@doctrinebot commented on GitHub (Dec 3, 2013):

Comment created by @ocramius:

[~intel352] did you check if the EBNF allows that?

@doctrinebot commented on GitHub (Dec 3, 2013): Comment created by @ocramius: [~intel352] did you check if the EBNF allows that?
Author
Owner

@doctrinebot commented on GitHub (Dec 3, 2013):

Comment created by intel352:

Per the doc link in my initial report, the docs say that the aggregation functions are allowed in both SELECT and GROUP BY clauses.

{quote}The following aggregate functions are allowed in SELECT and GROUP BY clauses: AVG, COUNT, MIN, MAX, SUM{quote}

Searching EBNF in same page, one of items supported for GROUP BY is:
{quote}/** ResultVariable identifier usage of mapped field aliases (the "total" of "COUNT() AS total") */
ResultVariable = identifier{quote}

So I assume that means we'd have to move AVG, COUNT, MIN, MAX, SUM into the SELECT statement, using SELECT AS , and then GROUP BY ?

@doctrinebot commented on GitHub (Dec 3, 2013): Comment created by intel352: Per the doc link in my initial report, the docs say that the aggregation functions are allowed in both SELECT and GROUP BY clauses. {quote}The following aggregate functions are allowed in SELECT and GROUP BY clauses: AVG, COUNT, MIN, MAX, SUM{quote} Searching EBNF in same page, one of items supported for GROUP BY is: {quote}/*\* ResultVariable identifier usage of mapped field aliases (the "total" of "COUNT(_) AS total") *_/ ResultVariable = identifier{quote} So I assume that means we'd have to move AVG, COUNT, MIN, MAX, SUM into the SELECT statement, using SELECT <function> AS <var>, and then GROUP BY <var> ?
Author
Owner

@doctrinebot commented on GitHub (Dec 3, 2013):

Comment created by @ocramius:

[~intel352] I think the syntax allows that, but you should give it a try. I don't see how that is supposed to work given how GROUP BY works in various RDBMS implementations

@doctrinebot commented on GitHub (Dec 3, 2013): Comment created by @ocramius: [~intel352] I think the syntax allows that, but you should give it a try. I don't see how that is supposed to work given how GROUP BY works in various RDBMS implementations
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2981