• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deleting duplicate rows using sql query

 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch , Srikanth Nalam
 
Srikanth Nalam
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Campbell
 
Jan Cumps
Bartender
Posts: 2576
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic