Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

delete duplicate records

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i m using oracle 9i. then what shall be the delete query?
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic