Removing duplicate rows from SQL statement techniques

Source: Internet
Author: User

To remove duplicate row data in the table, you may immediately think of the disintct keyword, but disintct can only be used to remove all columns in the table is the same row, if you encounter the need to remove multiple fields in the table repeating rows (that is, part of the same, part is not the same), So how do we do that? Through years of experience in compiling databases, I have compiled the following methods for your reference and use.

Method 1: Apply to return fewer fields

Select F1,f2,f3,MAX (F4) from TABLENAME GROUP by f1,f2,f3

Method 2: Apply to all fields of the return row, you need to specify a different field

SELECT * from TABLENAME T where F4= (select MAX (F4) from TABLENAME where T.f1=f1 and T.f2=f2 and T.F3=F3)

Method 3: Applies to all fields that return rows, you need to specify a different field "find the rows to be removed"

Select t1.* from TABLENAME as T1, (select F1,f2 , MAX (F3)As F3 from TABLENAME GROUP by F1,F2 have COUNT (*) >1) as T2where t1.f3<t2.f3and T1. F1=t2. F1 and T1. F2=t2. F2

Original address: http://www.zuowenjun.cn/post/2014/08/02/10.html

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.