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

delete duplicate value Expand / Collapse
Author
Message
Posted Wednesday, November 14, 2007 3:45 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 12, 2008 2:36 AM
Points: 230, Visits: 31
How to delete the duplicate value in the table..
Post #422010
Posted Wednesday, November 14, 2007 3:54 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 26, 2012 5:26 AM
Points: 1,367, Visits: 1,585
I assume that this table does not have a primary key :) (it is bad, but it is of course another story)

You could build a new table with only the unique values (select distinct * from oldtable), and then replace the old table. (for replacing you may want to look at sp_rename.


Regards,
Andras




Andras Belokosztolszki, MCPD, PhD
GoldenGate Software
Post #422015
Posted Wednesday, November 14, 2007 3:58 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, September 12, 2008 2:36 AM
Points: 230, Visits: 31
Hi
Please tell me how to delete when no primary key..

Regards..
karthikeyan MCAD
Post #422017
Posted Wednesday, November 14, 2007 4:01 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 26, 2012 5:26 AM
Points: 1,367, Visits: 1,585
If there is a duplicate row, and you only want to delete one of them (or only a certain number if there are more), then you can do something ugly like:

set rowcount 1
GO
delete from ..... where col1=value1 AND col2=vlaue2 AND ...
GO
set rowcount 0
GO


the rowcount 1 will ensure that only one row is deleted, even if there are many rows matching the where clause.

Regards,
Andras




Andras Belokosztolszki, MCPD, PhD
GoldenGate Software
Post #422019
Posted Wednesday, November 14, 2007 9:42 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, February 03, 2012 6:50 AM
Points: 1,336, Visits: 1,888
karthikeyan (11/14/2007)
Hi
Please tell me how to delete when no primary key..

Regards..
karthikeyan MCAD


Karthikeyan
This is probably not the mose elegant way to do this, but it works for a job I run every night to delete dupes where all fields are the same other than the identity column, that is why I suggested you add an identity column. I have a flaw in my front end app that allows for dupes to be entered, and I have not had time to deal with yet. I would *strongly* suggest you dump your production table into a temp table to test. You will have to add all the columns you want to look at in the where clauses.
--First create a test table
IF OBJECT_ID('TempDB..#Test','u') IS NOT NULL
DROP TABLE #Test
CREATE TABLE #Test
(
Col1 VARCHAR(20),
Col2 VARCHAR(20),
Col3 VARCHAR(20),
Col4 VARCHAR(20),
Col5 VARCHAR(20)
)
GO
--Then insert the test date, with lines 6 and 7 being dupes
INSERT INTO #Test
SELECT 'Blue','is','the','first','pair' UNION ALL
SELECT 'Orange','is','the','second','pair' UNION ALL
SELECT 'Green','is','the','third','pair' UNION ALL
SELECT 'Brown','is','the','fourth','pair' UNION ALL
SELECT 'Slate','is','the','fifth','pair' UNION ALL
SELECT 'Orange','is','the','second','pair' UNION ALL
SELECT 'Brown','is','the','fourth','pair'
GO
--Add an identity column to the table for use in deleting the dupes
ALTER TABLE #test
ADD ID INT IDENTITY(1,1)

GO
--Check #test first
SELECT *
FROM #test
--Delete the dupes
DELETE t1
FROM #test t1,
(--Select the MIN ID of the dupe records. It only stands to reason
--these were in the table first
SELECT
MIN(t1.id)ID ,t1.col1,t1.col2,t1.col3,t1.col4,t1.col5
FROM
(--Derived table t1 selects all the dupes, based on the columns you specify
--you would need to add any columns you want to look at as being criteria
--for dupe records
SELECT
t1.*
FROM #test t1,
#test t2
WHERE t1.col1 = t2.col1
AND t1.col2 = t2.col2
AND t1.col3 = t2.col3
AND t1.col4 = t2.col4
AND t1.col5 = t2.col5
AND t1.id <> t2.id
) t1,
#test t2
WHERE t1.col1 = t2.col1
AND t1.id <> t2.id
GROUP BY t1.col1,t1.col2,t1.col3,t1.col4,t1.col5
) t2
WHERE t1.col1 = t2.col1
AND t1.col2 = t2.col2
AND t1.col3 = t2.col3
AND t1.col4 = t2.col4
AND t1.col5 = t2.col5
AND t1.id <> t2.id
--Check #test after deletion
SELECT *
FROM #test

Hope this helps
Greg


Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Post #422179
Posted Wednesday, November 14, 2007 7:32 PM


Keeper of the Duck

Keeper of the Duck

Group: Moderators
Last Login: Wednesday, February 01, 2012 8:09 AM
Points: 6,524, Visits: 1,698
If there is no primary key, another way is to SELECT DISTINCT into a temporary table, delete the original table and then INSERT the values back into the original table.


K. Brian Kelley, CISA, MCSE, Security+, MVP - SQL Server
Regular Columnist (Security), SQLServerCentral.com
Author of Introduction to SQL Server: Basic Skills for Any SQL Server User
| Professional Site | Blog | View Brian Kelley's LinkedIn profile | Twitter
Post #422419
Posted Wednesday, November 14, 2007 9:30 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, February 02, 2012 6:21 AM
Points: 238, Visits: 915
When I tried an approach (just messing around) similar to the
col1=val1 and col2=val2....
when one of the cols had nulls then it doesn't equate to another null so the row remains. Unless you want to get more complex and ugly by adding double checks for each col one for equality and another for IS NULL.

I think using set-related approach is much better.
Post #422443
Posted Friday, November 16, 2007 7:09 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Yesterday @ 2:10 AM
Points: 4, Visits: 71
toniupstny (11/14/2007)
When I tried an approach (just messing around) similar to the
col1=val1 and col2=val2....
when one of the cols had nulls then it doesn't equate to another null so the row remains. Unless you want to get more complex and ugly by adding double checks for each col one for equality and another for IS NULL.

I think using set-related approach is much better.


You can use COALESCE(col1, '') = COALESCE(val1, '') and ...
Post #423043
Posted Friday, November 16, 2007 1:59 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, February 02, 2012 6:21 AM
Points: 238, Visits: 915
Oh... and if you want to keep the same identity numbers SET IDENTITY_INSERT tablename ON before doing the copy out and recopy then SET IDENTITY_INSERT ... OFF when all done.
Post #423223
Posted Wednesday, January 27, 2010 5:40 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
-- Two methods I really like (and plagiarized from people on here).

create table #ttemp
(cola int not null)

insert into #ttemp (cola) values (1)
insert into #ttemp (cola) values (1)
insert into #ttemp (cola) values (1)
insert into #ttemp (cola) values (2)
insert into #ttemp (cola) values (2)
insert into #ttemp (cola) values (2)
insert into #ttemp (cola) values (2)
insert into #ttemp (cola) values (2)
insert into #ttemp (cola) values (3)
insert into #ttemp (cola) values (4)
insert into #ttemp (cola) values (4)

select * from #ttemp

-- method 1
delete a
from #ttemp a
where %%LockRes%% <> (SELECT Min(%%LockRes%%)
FROM #ttemp t1
where t1.cola = a.cola)

-- method 2
--; WITH #ttempCTE
--AS
--(
-- SELECT ROW_NUMBER() OVER( PARTITION BY cola ORDER BY cola ) AS RowNumber
-- FROM #ttemp
--)
--DELETE
--FROM #ttempCTE
--WHERE RowNumber != 1

select * from #ttemp

drop table #ttemp
Post #854845
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse