O & M Technology-SQL statement for deleting duplicate records

Source: Internet
Author: User
A few months ago, when I called a senior friend, I asked a question: how can I delete all repeated records in a table? At that time, I never figured it out. Recently, I started learning ORACLESQL again. I thought about this problem and spent a lot of time thinking about it. Finally, I thought of a solution with my colleagues' reminders. SQL & gt; select * fromtemp; AABB & mdash; & mda

A few months ago, when I called a senior friend, I asked a question: how can I delete all repeated records in a table? At that time, I never figured it out.
I recently started to learn oracle SQL again. I thought about this problem and spent a lot of time thinking about it. Finally, I thought of a solution with my colleague's reminder.
SQL> select * from temp;

AA BB
------------
Abc
Abc
Xyz
Xyz
Opq

SQL> select max (rowid), aa, bb from temp group by aa, bb;

MAX (ROWID) AA BB
------------------
AAAMWPAABAAANlqAAB abc
AAAMWPAABAAANlqAAE opq
AAAMWPAABAAANlqAAD xyz

SQL> delete from temp where (rowid, aa, bb) not in (select max (rowid), aa, bb from temp group by aa, bb );

2 rows deleted.

SQL> commit;

Commit complete.

SQL> select * from temp;

AA BB
------------
Abc
Xyz
Opq

OK. After testing, the problem is solved. :) Happy ing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.