MSSQL Doesn't support grouping by an aliased variable #4946

Closed
opened 2026-01-22 14:53:38 +01:00 by admin · 1 comment
Owner

Originally created by @gnat42 on GitHub (Dec 18, 2015).

The following SQL is invalid for SQL SRV.

SELECT YEAR(someDate) as theYear FROM tablename GROUP BY theYear

However this is valid

SELECT YEAR(someDate) as theYear FROM tablename GROUP BY YEAR(someDate)

Unfortunately due to #5561 you cannot write a DQL query like the above. Also because of that SQL difference queries now need to care about the platform unless doctrine would take the variable and substitute the calculation instead for SQL SRV.

Originally created by @gnat42 on GitHub (Dec 18, 2015). The following SQL is invalid for SQL SRV. ``` SELECT YEAR(someDate) as theYear FROM tablename GROUP BY theYear ``` However this is valid ``` SELECT YEAR(someDate) as theYear FROM tablename GROUP BY YEAR(someDate) ``` Unfortunately due to #5561 you cannot write a DQL query like the above. Also because of that SQL difference queries now need to care about the platform unless doctrine would take the variable and substitute the calculation instead for SQL SRV.
admin closed this issue 2026-01-22 14:53:38 +01:00
Author
Owner

@billschaller commented on GitHub (Dec 18, 2015):

You can use a named native query to accomplish this.

See http://docs.doctrine-project.org/en/latest/reference/native-sql.html#named-native-query

@billschaller commented on GitHub (Dec 18, 2015): You can use a named native query to accomplish this. See http://docs.doctrine-project.org/en/latest/reference/native-sql.html#named-native-query
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#4946