Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

delete duplicate records

 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've an APR_MGR table with columns as mgr_id, flag, and backupid. thr is no primary key constraints in the table. the data is like below:

MgrID flag backupid
101 N null
101 N null
101 N null
102 Y 1111
103 Y 2222

now, i wud like to write a delete query, that will delete all the duplicate records of MgrID 101, except the first one.

i've written the below query:

delete from APR_MGR where MgrID = 101 and flag = 'N' and 1 < (select count(MgrID) from APR_MGR where MgrID = 101 and flag = 'N')

i was expecting 2 records to be deleted of MgrID 101, but it deleted all the 3 records.

what shall be the correct query?
[ August 30, 2008: Message edited by: vikas sharmaa ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikas,
What database are you using? If it is one that supports the concept of a row number, you can write a query that keeps the "first" one intact.

If it does not support the concept of a row number, the procedure is more steps. You will have to copy the distinct records into a new table, delete all the records from this table and then copy the records back into this table.
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m using oracle 9i. then what shall be the delete query?
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikas,
This has nothing to do with JDBC of-course :-)
(NOTE: Untested - and check the syntax also :-)

Good Luck,
Avi.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vikas,
It's easier to do this in Oracle than some other databases so this is good. See this wiki page sql for an example.

It's ok the question isn't about JDBC. It is about SQL making it on topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic