Skip to content
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

Wrong SQL generation with group by and Count() in EF Core 2.1 Preview 1 #11322

Closed
HSchwichtenberg opened this issue Mar 19, 2018 · 1 comment
Closed

Comments

@HSchwichtenberg
Copy link

HSchwichtenberg commented Mar 19, 2018

In EF Core 2.1 Preview 1, this query

var groups = (from p in ctx.FlightSet
                   group p by p.Departure into g
                   select new { City = g.Key, Count = g.Count() }).Where(x => x.Count > 5).OrderBy(x => x.Count).ToList();

produces this SQL

SELECT [p].[Departure], COUNT(*)
FROM [Flight] AS [p]
WHERE COUNT(*) > 5
GROUP BY [p].[Departure]

which is wrong.

Exception:

System.Data.SqlClient.SqlException: 'An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.'

Further technical details

EF Core version: 2.1.0-preview1-final
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 1703
SQL Server: 2017
IDE: Visual Studio 2017 15.6.1

@ajcvickers ajcvickers added this to the 2.1.0 milestone Mar 19, 2018
@smitpatel
Copy link
Member

Generated SQL in nightly build

      SELECT [p].[Departure] AS [City], COUNT(*) AS [Count]
      FROM [FlightSet] AS [p]
      GROUP BY [p].[Departure]
      HAVING COUNT(*) > 5
      ORDER BY [Count]

Duplicate of #10870

@smitpatel smitpatel removed this from the 2.1.0 milestone Mar 20, 2018
@smitpatel smitpatel removed their assignment Mar 20, 2018
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants