Microsoft
Software
Hardware
Network
Question : Column 'xxx' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
Hi Experts,
this is a simple beginner's mistake, but I just don't figure out how to solve it with my MSSQL 2008 database...
I have a table with the following structure (abbreviated, just the relevant fields shown):
[PersonGUID] [uniqueidentifier]
[Name] [varchar]
[Firstname] [varchar]
[City] [varchar]
[Notes] [text]
I would now like to SELECT all fields of all records where the combination of Name + ' ' + Firstname occurs more than once (to present a list of potential duplicates; only Name and Firstname are relevant, PersonGUID _is_ always different, City and Notes could be different, but all fields need to be included in the SELECT output). I have tried this statement
SELECT *
FROM Persons
GROUP BY Name, Firstname
HAVING COUNT(Name + ' ' + Firstname)>1
ORDER BY Name, Firstname;
but that produces the above error message, that PersonGUID is not part of an aggregate function or the GROUP-BY clause. It works somewhat if I only SELECT Name, Firstname, but I need the other fields, too.
I could, of course, add all fields to the GROUP-By clause, but that would then dramatically change the output of my list, as at least PersonGUID is different for each record.
How would you solve this?
Thanks a lot for your advice,
Thomas
Answer : Column 'xxx' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
Try this:
SELECT *
FROM (
SELECT *, count(*) OVER ( PARTITION BY Name, Firstname) cnt
FROM Persons ) temp
WHERE cnt > 1
Random Solutions
OL2k3: Unable to open your default e-mail folders. The information store could not be opened.
OLE DB provider 'SQLOLEDB' was unable to begin a distributed transaction
Disable / enable custom menu items
Internet Explorer is broken!!!
Custom Order By
Adding more rules to outlook - third part or outlook options
SQL Server BIT field true and false
Using DSum and Date Parameters in a Query Together
BAD WEEK NUMBER IN OUTLOOK CALENDAR
Send/receive fax via ms access