Meaningless Drivel is fun!*
The moose likes JDBC and the fly likes Deleting duplicate rows using sql query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Deleting duplicate rows using sql query" Watch "Deleting duplicate rows using sql query" New topic
Author

Deleting duplicate rows using sql query

Eshwar Prasad
Ranch Hand

Joined: Mar 21, 2008
Posts: 202

I have a requirement to delete rows using sql query in oracle database. Actually for instance i have employee table having empname and emplocation.

When i write the sql statement as "select * from employee" i get the below results

empname emplocation

user1 india
user2 india
user1 india
user1 india
user3 india
user1 india
user2 india

i have a requirement to delete multiple occurrence of user1 and user2 and just retain one copy of the record.

so my final result set should have

user1 india
user2 india
user3 india

please let me know how can i do this. i tried using distinct, count etc but not meeting the requirement as above.

Your suggestions would be precious
Srikanth Nalam
Greenhorn

Joined: Feb 23, 2010
Posts: 20
Please mention the exact query you have used.
If you use select distinct empname, emplocation from employee you will get unique records with unique combination of these columns.


Thanks & Regards,
Srikanth Nalam.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Welcome to the Ranch , Srikanth Nalam
Srikanth Nalam
Greenhorn

Joined: Feb 23, 2010
Posts: 20
Thanks Campbell
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

Srikanth,

this query might give you a starting point to remove the duplicates. Oracle has a concept called rowid. It gives all your records a unique identifier.
This rowid should typically not be used in any query (it is a relational database after all, we should never address a record this way), but it can help you in this occasion.
You have to find all records that have the same values in all columns (duplicates), and only keep one of them.
In my example, I have kept the one with the lowest rowid.



Ensure that you understand the concept before trying it.
Test first on a test database!
Make a backup!
Etc..!


Regards, Jan

OCUP UML fundamental and ITIL foundation
youtube channel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deleting duplicate rows using sql query