Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 deleting cells with repeat data

Author  Topic 

liagrisj
Starting Member

9 Posts

Posted - 2010-03-12 : 12:14:35
I have a list of drivers where many of the cells, in one column, have the same name in them. How would I select those two 'bob smith' out and delete them? I should specify that I don't want to actually put the name 'bob smith' in the query. I want the query to find the duplicates.

example:

bob smith
jane doe
bob smith
eric brown

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-12 : 12:16:19
[code]SELECT NameField
FROM YourTable
GROUP BY NameField
HAVING COUNT(*) >1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

liagrisj
Starting Member

9 Posts

Posted - 2010-03-12 : 13:53:57
Thanks. This works well. How would I go about deleting them? Also, is it possible to delete all instances except for one? Example: I have four 'Bob Smith' and I want to delete three of them with one remaining.

SELECT driverName
FROM tblDriver
GROUP BY driverName
HAVING (COUNT(*) > 4)
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2010-03-12 : 14:09:31
PL. DO TAKE A BACKUP BEFORE RUNNING THIS SCRIPT
;WITH [CTE DUPLICATE] AS 
(
SELECT
RN = ROW_NUMBER() OVER (PARTITION BY driverName ORDER BY driverName DESC),
driverName
FROM tblDriver
)
DELETE FROM [CTE DUPLICATE] WHERE RN > 1
Go to Top of Page

namman
Constraint Violating Yak Guru

285 Posts

Posted - 2010-03-12 : 20:11:54
Also, is it possible to delete all instances except for one?


yes

1- create a temp table variable with the same structure as your table.
2- insert duplicated rows into temp table.
3- delete duplicated rows from your table.
4- insert duplicated rows into your table from temp table.


run example below to see if it works as you want.


declare @temp table(id int, name varchar(50))

--- example table -----
declare @yourTable table(id int, name varchar(50))
insert into @yourTable values(1, 'bob smith')
insert into @yourTable values(12, 'jane doe')
insert into @yourTable values(12, 'bob smith')
insert into @yourTable values(11, 'john deer')
insert into @yourTable values(10, 'jane doe')
insert into @yourTable values(2, 'jane doe')
insert into @yourTable values(3, 'eric brown')
insert into @yourTable values(23, 'jane doe')
-- original values -------
select * from @yourTable order by name

;with temp as
(
select *, ROW_NUMBER() over(PARTITION by name order by name) as rowid
from @yourTable
)
insert into @temp select id, name from temp where rowid = 2
delete @yourTable where name in (select name from @temp)
insert into @yourTable select * from @temp

-- after remove ------
select * from @yourTable order by name


REUSLT:

id name
----------- ----------------------------
1 bob smith
12 bob smith
3 eric brown
10 jane doe
2 jane doe
23 jane doe
12 jane doe
11 john deer


id name
----------- -----------------------------
12 bob smith
3 eric brown
2 jane doe
11 john deer



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-13 : 00:52:10
quote:
Originally posted by namman

Also, is it possible to delete all instances except for one?


yes

1- create a temp table variable with the same structure as your table.
2- insert duplicated rows into temp table.
3- delete duplicated rows from your table.
4- insert duplicated rows into your table from temp table.


run example below to see if it works as you want.


declare @temp table(id int, name varchar(50))

--- example table -----
declare @yourTable table(id int, name varchar(50))
insert into @yourTable values(1, 'bob smith')
insert into @yourTable values(12, 'jane doe')
insert into @yourTable values(12, 'bob smith')
insert into @yourTable values(11, 'john deer')
insert into @yourTable values(10, 'jane doe')
insert into @yourTable values(2, 'jane doe')
insert into @yourTable values(3, 'eric brown')
insert into @yourTable values(23, 'jane doe')
-- original values -------
select * from @yourTable order by name

;with temp as
(
select *, ROW_NUMBER() over(PARTITION by name order by name) as rowid
from @yourTable
)
insert into @temp select id, name from temp where rowid = 2
delete @yourTable where name in (select name from @temp)
insert into @yourTable select * from @temp

-- after remove ------
select * from @yourTable order by name


REUSLT:

id name
----------- ----------------------------
1 bob smith
12 bob smith
3 eric brown
10 jane doe
2 jane doe
23 jane doe
12 jane doe
11 john deer


id name
----------- -----------------------------
12 bob smith
3 eric brown
2 jane doe
11 john deer






no need of all these. you can just do

DELETE t
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY name order by id) as rowid
from @yourTable)t
WHERE rowid >1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

liagrisj
Starting Member

9 Posts

Posted - 2010-03-13 : 09:09:25
Thanks! I will check these ideas out Monday morning.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-13 : 11:05:31
good luck
lets us know your outcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

liagrisj
Starting Member

9 Posts

Posted - 2010-03-15 : 08:45:12
Sorry, I'm bad at sql since I never use it. I can't get the above code to run. Here is a snippet of the table I'm working with. Also, is there a way to do a select to show what's going to be deleted before I execute the delete statement? I've backed up the database before all my deletes.

----------------------------------------
driverID | driverName | driverCDL
----------------------------------------
2 | Bob Smith | 05401246
----------------------------------------
3 | Jane Doe | 48983468
----------------------------------------
4 | Eric Brown | 65498813

Thanks again for all your help.
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-15 : 09:09:47
quote:
Originally posted by liagrisj

Sorry, I'm bad at sql since I never use it. I can't get the above code to run. Here is a snippet of the table I'm working with. Also, is there a way to do a select to show what's going to be deleted before I execute the delete statement? I've backed up the database before all my deletes.

----------------------------------------
driverID | driverName | driverCDL
----------------------------------------
2 | Bob Smith | 05401246
----------------------------------------
3 | Jane Doe | 48983468
----------------------------------------
4 | Eric Brown | 65498813

Thanks again for all your help.



Yup.This is modified of visakh response..for op request i have changed this..

please try this.
select * from
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY name order by id) as rowid
from @yourTable)t
WHERE rowid >1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-15 : 09:40:37
quote:
Originally posted by liagrisj

Sorry, I'm bad at sql since I never use it. I can't get the above code to run. Here is a snippet of the table I'm working with. Also, is there a way to do a select to show what's going to be deleted before I execute the delete statement? I've backed up the database before all my deletes.

----------------------------------------
driverID | driverName | driverCDL
----------------------------------------
2 | Bob Smith | 05401246
----------------------------------------
3 | Jane Doe | 48983468
----------------------------------------
4 | Eric Brown | 65498813

Thanks again for all your help.



why cant you get code to run? whats the error you got? did you replace your actual column names in my suggestion?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

liagrisj
Starting Member

9 Posts

Posted - 2010-03-15 : 09:59:06

DELETE t
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY drivername order by driverid) as driverid
from @tbldriver)t
WHERE driverid >1


This is how I've changed it. When run, it states row_number is not a valid function. Obviously, I've screwed this up. I'm not sure which columns to put where? I only have three columns as shown in prior post.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-15 : 10:02:19
quote:
Originally posted by liagrisj


DELETE t
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY drivername order by driverid) as driverid
from @tbldriver)t
WHERE driverid >1


This is how I've changed it. When run, it states row_number is not a valid function. Obviously, I've screwed this up. I'm not sure which columns to put where? I only have three columns as shown in prior post.


whats the sql server version you're using? ROW_NUMBER only works from SQL 2005 onwards

please run below and post back result

SELECT @@VERSION

EXEC sp_dbcmptlevel 'your db name'

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

liagrisj
Starting Member

9 Posts

Posted - 2010-03-15 : 11:20:57
Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)
May 3 2005 23:18:38
Copyright (c) 1988-2003 Microsoft Corporation
Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 1)

We're upgrading to 2005 within the month. But we're still on the old for now.
Go to Top of Page

haroon2k9
Constraint Violating Yak Guru

328 Posts

Posted - 2010-03-15 : 11:29:08
quote:
Originally posted by liagrisj

Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)
May 3 2005 23:18:38
Copyright (c) 1988-2003 Microsoft Corporation
Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 1)

We're upgrading to 2005 within the month. But we're still on the old for now.


to see the duplicates.please run this..this is visakh reply earlier..
SELECT NameField
FROM YourTable
GROUP BY NameField
HAVING COUNT(*) >1

1.please say how many records have shown duplicates by running above query
2.Total records in your table
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-15 : 11:34:08
quote:
Originally posted by liagrisj

Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)
May 3 2005 23:18:38
Copyright (c) 1988-2003 Microsoft Corporation
Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 1)

We're upgrading to 2005 within the month. But we're still on the old for now.


so its 2000. thats why it didnt work. use this 2000 solution then


DELETE t
FROM Table t
LEFT JOIN
(SELECT drivername,MIN(driverid) as mindriver
from Table
GROUP BY drivername)t1
ON t1.drivername = t.drivername
AND t1.mindriver = t.driverid
WHERE t1.drivername IS NULL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

liagrisj
Starting Member

9 Posts

Posted - 2010-03-15 : 12:52:16
That worked perfectly! I can't thank you guys enough. You've saved me hours of tedious labor.

Take care.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-15 : 12:57:18
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -