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
 how to duplicate records deleted?

Author  Topic 

maifs
Yak Posting Veteran

57 Posts

Posted - 2009-11-26 : 07:50:35
how to delete the duplicate records from a table?
like Item is a table and its columns are Item(IID,Iname,Price)
how to?
as

i want to delete duplicate records.

e.g:

IID Iname Price
1 alo 34
2 dhi 48
3 banana 68
3 banana 68
4 apple 50

how to remove these records?
3 banana 68
3 banana 68

and if IID was a primary key?
3 banana 68
4 banana 68
then 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 t1
GROUP BY col1, col2
HAVING count(*) > 1


-------------------------
R...
Go to Top of Page

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.


Go to Top of Page

oguzkaygun
Yak Posting Veteran

53 Posts

Posted - 2009-11-26 : 08:32:02
Delete from table where name = 'banana 68'
Go to Top of Page

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 ID

DELETE f
FROM
(
SELECT
[ID]
, [a] AS [a]
, ROW_NUMBER() OVER(PARTITION BY [a] ORDER BY [ID]) AS [pos]
FROM
@foo
)
f
WHERE
f.[pos] > 1

SELECT * FROM @foo



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -