Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

delete duplicates Expand / Collapse
Author
Message
Posted Monday, February 9, 2009 3:33 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, November 25, 2009 4:57 PM
Points: 74, Visits: 550
Hi Every body,

I have one table Employees

EmpID EmpName
1 aaa
1 aaa
2 aaa
3 bbb
2 bbb

Here in my table dont have primary key to that EMPID how can i eliminate duplicates in my table am using self joins but it is not working could any body help me plz really appreciate to you

Thanks
Post #652644
Posted Monday, February 9, 2009 3:39 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, June 20, 2013 2:04 AM
Points: 222, Visits: 331
A good method for deleting duplicates is using CTE.

it is nicely explained in the msdn site. http://msdn.microsoft.com/en-us/library/ms190766(SQL.90).aspx

Hope the above helps.

Sriram


Sriram

Post #652650
Posted Monday, February 9, 2009 3:43 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 4:53 AM
Points: 2,366, Visits: 1,844
One common method is to move the data into another table (same structure as emp table)and drop the original emp table and rename the new table.

Create table NewEmp.....

SELECT DISTINCT EmpID,Empname INTO NewEmp
From Emp

Drop Emp

sp_rename 'NewEmp' , 'Emp'


"Keep Trying"
Post #652654
Posted Monday, February 9, 2009 3:51 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, November 25, 2009 4:57 PM
Points: 74, Visits: 550
Hi Thank you for ur reply,


in that EMPID dont have primary key is it possible that one because it is very big database plz help me
Post #652663
Posted Monday, February 9, 2009 7:14 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,556, Visits: 2,593
Here is a CTE based solution....

; WITH EmployeesCTE
AS
(
SELECT ROW_NUMBER() OVER( PARTITION BY EmpID, EmpName ORDER BY EmpID ) AS RowNumber,
EmpID, EmpName
FROM Employees
)
DELETE
FROM EmployeesCTE
WHERE RowNumber != 1



--Ramesh

Post #652795
Posted Monday, February 9, 2009 8:58 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, October 25, 2010 6:09 AM
Points: 1,621, Visits: 409
if the table is small then create a new table with the distinct values and then drop the old table and rename the new table to the Old table name.

Thanks -- Vijaya Kadiyala
http://dotnetvj.blogspot.com


Thanks -- Vijaya Kadiyala
www.dotnetvj.com
SQL Server Articles For Beginers



Post #652894
Posted Wednesday, January 27, 2010 5:10 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, February 18, 2010 5:15 PM
Points: 6, Visits: 8
Here is a CTE based solution....

; WITH EmployeesCTE
AS
(
SELECT ROW_NUMBER() OVER( PARTITION BY EmpID, EmpName ORDER BY EmpID ) AS RowNumber,
EmpID, EmpName
FROM Employees
)
DELETE
FROM EmployeesCTE
WHERE RowNumber != 1



--Ramesh

This is elegant...
Post #854835
Posted Wednesday, January 27, 2010 5:23 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 1:51 PM
Points: 21,644, Visits: 15,317
There are already a couple of solutions proposed that use a CTE. I would recommend picking a CTE solution.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #854836
Posted Wednesday, January 27, 2010 10:56 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 11:34 PM
Points: 790, Visits: 646
DECLARE @tbl TABLE
(
EMPID INT,
EmpName VARCHAR(100)
)

INSERT INTO @tbl
SELECT
1, 'aaa'
UNION ALL
SELECT
1, 'aaa'
UNION ALL
SELECT
2, 'aaa'
UNION ALL
SELECT
3, 'bbb'
UNION ALL
SELECT
2, 'bbb'



DELETE D FROM
(
SELECT
ROW_NUMBER() OVER(PARTITION BY EMPID,EmpName ORDER BY EMPID) AS ROWS,
EMPID,
EmpName
FROM
@tbl
)D
WHERE
ROWS > 1

SELECT * FROM @tbl



Regards,
Mitesh OSwal
+918698619998
Post #854923
Posted Thursday, January 28, 2010 2:21 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 1:51 AM
Points: 1,262, Visits: 3,422
Yes the CTE technique is the best to eliminate your duplicate values...so just do it!


============================================================
SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #855005
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse