| 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 brownThanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-12 : 12:16:19
|
| [code]SELECT NameFieldFROM YourTableGROUP BY NameFieldHAVING COUNT(*) >1 [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 driverNameFROM tblDriverGROUP BY driverNameHAVING (COUNT(*) > 4) |
 |
|
|
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 |
 |
|
|
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?yes1- 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 = 2delete @yourTable where name in (select name from @temp)insert into @yourTable select * from @temp-- after remove ------select * from @yourTable order by nameREUSLT:id name----------- ----------------------------1 bob smith12 bob smith3 eric brown10 jane doe2 jane doe23 jane doe12 jane doe11 john deerid name----------- -----------------------------12 bob smith3 eric brown2 jane doe11 john deer |
 |
|
|
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?yes1- 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 = 2delete @yourTable where name in (select name from @temp)insert into @yourTable select * from @temp-- after remove ------select * from @yourTable order by nameREUSLT:id name----------- ----------------------------1 bob smith12 bob smith3 eric brown10 jane doe2 jane doe23 jane doe12 jane doe11 john deerid name----------- -----------------------------12 bob smith3 eric brown2 jane doe11 john deer
no need of all these. you can just doDELETE tFROM (SELECT ROW_NUMBER() OVER (PARTITION BY name order by id) as rowid from @yourTable)tWHERE rowid >1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
liagrisj
Starting Member
9 Posts |
Posted - 2010-03-13 : 09:09:25
|
| Thanks! I will check these ideas out Monday morning. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-13 : 11:05:31
|
| good lucklets us know your outcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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 | 65498813Thanks again for all your help. |
 |
|
|
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 | 65498813Thanks again for all your help.
Yup.This is modified of visakh response..for op request i have changed this..please try this.select * fromFROM (SELECT ROW_NUMBER() OVER (PARTITION BY name order by id) as rowid from @yourTable)tWHERE rowid >1 |
 |
|
|
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 | 65498813Thanks 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
|
liagrisj
Starting Member
9 Posts |
Posted - 2010-03-15 : 09:59:06
|
| DELETE tFROM (SELECT ROW_NUMBER() OVER (PARTITION BY drivername order by driverid) as driverid from @tbldriver)tWHERE driverid >1This 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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-15 : 10:02:19
|
quote: Originally posted by liagrisj DELETE tFROM (SELECT ROW_NUMBER() OVER (PARTITION BY drivername order by driverid) as driverid from @tbldriver)tWHERE driverid >1This 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 onwardsplease run below and post back resultSELECT @@VERSIONEXEC sp_dbcmptlevel 'your db name'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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. |
 |
|
|
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 NameFieldFROM YourTableGROUP BY NameFieldHAVING COUNT(*) >1 1.please say how many records have shown duplicates by running above query2.Total records in your table |
 |
|
|
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 thenDELETE tFROM Table tLEFT JOIN(SELECT drivername,MIN(driverid) as mindriverfrom Table GROUP BY drivername)t1ON t1.drivername = t.drivername AND t1.mindriver = t.driveridWHERE t1.drivername IS NULL ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
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. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-15 : 12:57:18
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|