Skip to content
Advertisement

Unfiltered data in a group by statement – SQL Server

I have created a SQL statement using group by and some filters but I received an unexpected result:

As we can see in the field brand we have code 1 and code 2 but in my query I already made a filter to get just 1:

I don’t know why I have this result. My expected result is this:

If I use having I resolve the problem in this example but in my real life problem I must not use having.

Here is the script of my table:

Advertisement

Answer

you are missing parentheses in the query

however here is a better way to do it

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement