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
Help with Hyper-V Planning for Intel Modular Server
URGENT - 2003 Server - this system does not permit you to logon interactivally
Windows Scripting: FileSystemObject properties inconsistent with Windows Explorer values
aspx/c# looping through data from within body
Problems when running VB.NET app as a scheduled task
I keep getting this message while I am on the google.com webpage: "Google would like to have access to your location" (see pic)
hide in form closing....can not shut down
Need to migrate off NT4 domain where in addition to NT4 server there are two 2003 servers and an SBS 2003 server on the network
SQL ORDER BY Case needed?
How can we display employee contact photos in the Office Communicator contact card