I have tried searching this site but to no avail. I do not know how to word my question quite properly, but here goes:
I have a table with employees from multiple companies in it. They all have a unique employee number (Num), but can belong to several different companies (Comp). The problem is that we have a separate entry for each company an employee is a part of. I need to be able to find every employee that has multiple entries, along with all of the companies (Comp) that they are a part of.
I cannot think of a solution short of separating all of the companies out into temp tables and comparing all of them for duplicate records.
Here is an example:
If I have the following in a table,
Comp Num FName MI LName
McD's 1 Dick H Harper
BrgrKng 1 Dick H Harper
Wendy's 2 Jane B Doe
Arby's 3 John G Doe
I want the following results:
Comp Num FName MI LName
McD's 1 Dick H Harper
BrgrKng 1 Dick H Harper
I have the following code:
SELECT Comp, Num, FName, MI, LName
FROM Employees
HAVING COUNT(Num) > 1
ORDER BY LName, FName, MI
This code does not work because of the HAVING clause and trying to display Comp as well as Num.
Whenever I try to run it I get the expected error message:
Column 'Employees.Comp' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Short of separating out all of the companies' employees and comparing every company to each, what can I do?
Side note:
None of the following entries helped:
How to Find Rows which are Duplicates by a Key but Not Duplicates in All Columns?