-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sum followed by a group by: "Cannot perform an aggregate function on an expression containing an aggregate or a subquery. " #28238
Comments
@cts-tradeit It's not clear to us how to make this LINQ query translate to SQL Server, since SQL Server does not support nested aggregates. What SQL do you expect to be generated here? |
@ajcvickers Yes, that's why it should NOT be translated as nested aggregates! One solution would be delegating one of the sums to a subquery
My current workround is a IQueryable that produces roughly this SQL (instead of subtracting Movements generally before summing Quantities, it subtract respective Movements of each type):
However, this approach is somewhat resource wasting as SQL Server repeatedly queries table Movements even though it could have been done all at once. Even if SQL Server supported nested aggregates, the generated SQL would be frankly quite resource wastefull the same way as my workaround is, it suffers from the same repeated quering of table Movements. |
@cts-tradeit Thanks for the additional analysis. Note from triage: currently blocked on #20291. |
Duplicate of #34256 |
Am going to use #34256 to track working around the SQL Server limitation in EF. |
This queryable:
Produces SQL query which is invalid:
However if we remove:
in the Select before GroupBy, we get:
It seems that the first Select is not used as sub-query but rather included within the SUMs from second SELECT (after .GroupBy()) which results in SUM(SELECT (Sum(...))).
EFC: 6.0.5
EFC.SqlServer: 6.0.5
The text was updated successfully, but these errors were encountered: