Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
how to delete the duplicate records from a table?like Item is a table and its columns are Item(IID,Iname,Price)how to?asi want to delete duplicate records.e.g:IID Iname Price1 alo 342 dhi 483 banana 683 banana 684 apple 50how to remove these records?3 banana 683 banana 68and if IID was a primary key?3 banana 684 banana 68then how to solve it?after one record ,all those records would be deleted which are repeated.
rajdaksha
Aged Yak Warrior
595 Posts
Posted - 2009-11-26 : 08:01:57
Hi Try like this..
create table t1(col1 int, col2 int, col3 char(50))insert into t1 values (1, 1, 'data value one')insert into t1 values (1, 1, 'data value one')insert into t1 values (1, 2, 'data value two')SELECT col1, col2, count(*)FROM t1GROUP BY col1, col2HAVING count(*) > 1
-------------------------R...
maifs
Yak Posting Veteran
57 Posts
Posted - 2009-11-26 : 08:15:47
quote:Originally posted by rajdaksha Hi i have to delete duplicate records not selecting.
oguzkaygun
Yak Posting Veteran
53 Posts
Posted - 2009-11-26 : 08:32:02
Delete from table where name = 'banana 68'
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts
Posted - 2009-11-26 : 12:06:17
Does this help?
DECLARE @foo TABLE ( [ID] INT , [a] VARCHAR(2) )INSERT @foo SELECT 1, 'a'UNION SELECT 2, 'a'UNION SELECT 3, 'a'UNION SELECT 4, 'b'SELECT * FROM @foo-- delete duplicates 'a' keeping lowest IDDELETE fFROM ( SELECT [ID] , [a] AS [a] , ROW_NUMBER() OVER(PARTITION BY [a] ORDER BY [ID]) AS [pos] FROM @foo ) fWHERE f.[pos] > 1SELECT * FROM @foo
Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION