473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Checking for duplicates in a database table

I'm trying to locate duplicate data in a table using 4 columns:
employee_id (primary key), employeeid, lastname and firstname.

I can pull up the duplicate data with the last three listed columns,
but when I include the first column (employee_id) in the statement, it
shows nothing, since the employee_id value is unique for each row.
Here's the statement I'm currently using:

select employeeid, firstname, lastname
from empmain
group by employeeid, firstname, lastname
having count(*) 1

The only thing I need extra from this statement is a separate row for
each duplicate value, showing the unique employee_id value. I know I
need to take the Group By option out, but I get the error "Column
'empmain.employeeid' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.".
Tried doing research on this issue, but honestly, my knowledge of SQL
is limited and I'm really not sure I'm asking the right questions when
searching. Any help would be appreciated.

- Travis
Jun 27 '08 #1
3 2517
On SQL Server 2005 you can do this:

WITH Dups
AS
(SELECT employee_id, employeeid, firstname, lastname,
COUNT(*) OVER(PARTITION BY employeeid, firstname, lastname) AS
cnt
FROM empmain)
SELECT employee_id, employeeid, firstname, lastname
FROM Dups
WHERE cnt 1;

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #2
Travis,

this is not too beautifull but at least it gives the desired result:

SELECT FirstName, LastName, EmployeeID
FROM Employees AS E1
WHERE FirstName IN (SELECT FirstName
FROM Employees AS E2
GROUP BY FirstName, LastName
HAVING Count(*) 1
AND LastName = E1.LastName)

The subquery filters out the duplicate names and connects it in the
outer query to the EmployeeID.

Brgds

Philipp Post
Jun 27 '08 #3
(st******@gmail.com) writes:
I'm trying to locate duplicate data in a table using 4 columns:
employee_id (primary key), employeeid, lastname and firstname.

I can pull up the duplicate data with the last three listed columns,
but when I include the first column (employee_id) in the statement, it
shows nothing, since the employee_id value is unique for each row.
Here's the statement I'm currently using:
It seems that that employee_id column should not be there, but there
should be a real key in the table.
select employeeid, firstname, lastname
from empmain
group by employeeid, firstname, lastname
having count(*) 1

The only thing I need extra from this statement is a separate row for
each duplicate value, showing the unique employee_id value. I know I
need to take the Group By option out, but I get the error "Column
'empmain.employeeid' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.".
Tried doing research on this issue, but honestly, my knowledge of SQL
is limited and I'm really not sure I'm asking the right questions when
searching. Any help would be appreciated.
On SQL 2005 you can do:

WITH counts AS (
SELECT employee_id, employeeid, firstname, lastname,
cnt = COUNT(*)
OVER (PARTITION BY employeeid, firstname, lastname)
FROM empmain
)
SELECT employee_id, employeeid, firstname, lastname
FROM counts
WHERE cnt 1

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: keys4worship | last post by:
Situation: Day 1 Table contains 100 items of actions imported via FTP. One of the fields in the table can be updated to reflect an assigned unit code. Day 2 Actions that may be duplicates of...
4
by: Drew | last post by:
I have a permission tracking app that I am working on, and I have made the insert page for it. I am having issues on how to prevent duplicates from getting entered. Currently the interface for...
5
by: Jermin | last post by:
Hi, I have a database composed of a single table composed of 2 columns, an auto-numbered ID column and a column which contains 30 million random numbers. All I want to Access to do is check the...
1
by: MHenry | last post by:
Hi, I have a table with duplicate records. Some of the duplicates need to be eliminated from the table and some need not. A duplicate record does not need to be eliminated if the one record...
6
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec...
6
by: Maxi | last post by:
I have 100 tabes in an Access database, every table has 1 filed with 100 names (records), no primary key assigned. I would like to find duplicates. Here is the criteria: The computer should...
3
by: AK | last post by:
Hi Our product uses MS-SQL Server 2000. One of our customer has 10 installations with each installation stroring data in its own database. Now the customer wants to consolidate these databases...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
2
by: lostdawg | last post by:
Hi, I have a question regarding duplicate entries. Basically I have a simple database with a main table called contacts. This table has 2 fields, contactsid and contactsnum. The main form has a...
7
by: john.cole | last post by:
I have searched all the groups I can, and I still haven't been able to come up the solution I need. I have the following problem. In my form named sbfrmSpoolList, I am entering a job, spool and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.