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
Select Row Number
remove trailing comma from list
SQL Join Group and SUM
Outlook 2007 constantly asking for password in SBS 2008 domain
Could not load file or assembly 'Microsoft.SharePoint.Sear<wbr />ch' or one of its dependencies
how to use if statements for Excel
table link
Default Application Pool keeps stopping.
Combo box strange behavior
How to save Excel 2007 spreadsheets into a form readable by Lotus 123