943,846 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 49052
  • MS SQL RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 19th, 2007
0

how to delete duplicate record in a table by using SQL query

Expand Post »
how to delete duplicate record in a table by using SQL query
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
g.prabu is offline Offline
7 posts
since Feb 2007
Feb 19th, 2007
0

Re: how to delete duplicate record in a table by using SQL query

Click to Expand / Collapse  Quote originally posted by g.prabu ...
how to delete duplicate record in a table by using SQL query
duplicate of what? all duplicates? why don't you have a unique identifier? do you have any validation? why are you here? why am i answering you?
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Feb 20th, 2007
0

Re: how to delete duplicate record in a table by using SQL query

i mean if a row occurs twise we have to delete one row of that two rows. no validation. by using sql query . if u can send me that query
Reputation Points: 10
Solved Threads: 0
Newbie Poster
g.prabu is offline Offline
7 posts
since Feb 2007
Apr 9th, 2007
0

Re: how to delete duplicate record in a table by using SQL query

Click to Expand / Collapse  Quote originally posted by g.prabu ...
how to delete duplicate record in a table by using SQL query
I also faced the same kind of problem and solved it by myself as I never got good source.
I guess you have a table that has duplicate records but still you can identify them with a unique field, if so then follow this syntax, the theory is pretty simple, find the duplicate records with a subquery and then use the delete statement!!

for our query,I'm assuming that unique field is :U_F
the product code is " P_C
if you have multiple fields which you may have to match , then I am using another field which you may omit if you don't have and can increase the number of fields by adding them with AND.
so another field name i am using is : A_F

so the query will be:

DELETE *
FROM TableName WHERE U_F IN (select a.U_F from TableName as a, TableName AS b
WHERE a.P_C=b.P_C And a.A_F=b.A_F And a.U_F>b.U_F
and a.transaction_mode='some string/number');
'some string/number' represent the value that determines which records will be deleted!!
I hope this solves your problem!!! Mail me if you find this difficult and wanna get into more details!!
Bye
Arun
Reputation Points: 10
Solved Threads: 1
Newbie Poster
arunendra is offline Offline
1 posts
since Apr 2007
Apr 16th, 2007
0

Re: how to delete duplicate record in a table by using SQL query

Hi,

You can find methods summarized with samples at the article named "How to delete duplicate records or rows among identical rows in a table where no primary key exists" at http://www.kodyaz.com/articles/delet...n-a-table.aspx

Eralper
Reputation Points: 10
Solved Threads: 2
Newbie Poster
eralper is offline Offline
9 posts
since Aug 2005
May 18th, 2007
0

Re: how to delete duplicate record in a table by using SQL query

Hi,
I registered just to thank you for this amazing article.
The query using the identity is simple and elegant!


Thanks a lot.
Pino

Click to Expand / Collapse  Quote originally posted by eralper ...
Hi,

You can find methods summarized with samples at the article named "How to delete duplicate records or rows among identical rows in a table where no primary key exists" at http://www.kodyaz.com/articles/delet...n-a-table.aspx

Eralper
Reputation Points: 10
Solved Threads: 1
Newbie Poster
puppinoo is offline Offline
1 posts
since May 2007
May 19th, 2007
0

Re: how to delete duplicate record in a table by using SQL query

the simple method remove duplicate rows on the table using the following query
first find the rows which have duplicates
SELECT identity column(fid)
FROM table
GROUP BY fid
HAVING COUNT(*) > 1

and then
SET ROWCOUNT 1
DELETE FROM table
WHERE fid = 1 AND fid = 1
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bsasiju is offline Offline
1 posts
since May 2007
May 21st, 2007
0

Re: how to delete duplicate record in a table by using SQL query

only remove duplicate records, select duplicated fields, and then apply group by

create table #temp same schema
INSERT INTO #temp SELECT field1,field2,field{n} FROM yourtable GROUP BY field1,field2,field{n}

DELETE OLDdata
INSERT INTO yourtable select * from #temp
DROP #temp
recommend: in transaction
Reputation Points: 10
Solved Threads: 1
Newbie Poster
rado is offline Offline
4 posts
since May 2007
May 23rd, 2007
0

Re: how to delete duplicate record in a table by using SQL query

I have a table named "delivery" that has 10 fields. From that Iwant to count most delivered items, that means I want to Count duplicates based on ItemCode and Amount and group by shop names.

The looks like

ItemCode
Amount
Shopname
zz
zz
01
100
Telebrand


01
100
Telebrand


01
100
Telebrand


02
50
Telebrand


02
50
Telebrand




when I'll run the query it should give me data like this:

ShopNmae
Totaldelivery
Telebrand
4


that means duplicates will be counted on ItemCode and Amount. And most importantly it will always 1 less of each occurance. Like 01 and 100 occured 3 times but counted 2 and 02 and 50 occured 2 times but it will count 1 and the total count will be 4. And I have a field that is delivery data, Iwant to run this count operation within a fixed period of time using between comand or anything else. Can any of you please let me know the sql code for it ?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Nabi_Ahmad is offline Offline
1 posts
since May 2007
Mar 5th, 2008
0

Re: how to delete duplicate record in a table by using SQL query

Click to Expand / Collapse  Quote originally posted by rado ...
only remove duplicate records, select duplicated fields, and then apply group by

create table #temp same schema
INSERT INTO #temp SELECT field1,field2,field{n} FROM yourtable GROUP BY field1,field2,field{n}

DELETE OLDdata
INSERT INTO yourtable select * from #temp
DROP #temp
recommend: in transaction
Asalamvalikum
Hi This is mohammed azhar new to comm..

i need ur help how to update multiple columns (period,section)
in which data has interchanged

reply if possible today
Reputation Points: 10
Solved Threads: 1
Newbie Poster
mohammeds is offline Offline
3 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: MS SQL: Converting row printing to column based printing
Next Thread in MS SQL Forum Timeline: syntax query for backup database for one month in sql server 2000





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC