Several Methods for filtering duplicate data using SQL statements

Source: Internet
Author: User

During development, we often encounter data with repeated records. We try again to filter Repeated Records. Below I have summarized several methods to help you.

Method 1,

 

Select identity (int, 1, 1) as id, * into # temp from tabel
Select * from # temp where id in (
Select max (id) from # emp where having count (*)> 1 group by col1, col2, col3 ...)


Instance

The structure of a table is as follows:

Id bookname cbs zz
001 asp tutorial Daquan Tsinghua University Press House Tian zhengping
002 database tutorial system tutorial Zhou aoying, Higher Education Press
003 asp Daquan Tsinghua University Press House Tian zhengping

Now I want to make a query. The fields to be searched are as follows:

Bookname, cbs, zz

Filter records by the bookname field, but display multiple fields

The query result should be

Bookname cbs zz
Asp Daquan Tsinghua University Press House Tian zhengping
Database System tutorial Zhou aoying, Higher Education Press

However, I don't know how to write SQL statements.

 


Nononono (null, null): The method can solve the problem of completely repeating. If there are several fields that are not repeated
But what should I do?
For example, if you want to display
Id bookname cbs zz new_id
(New_id is repeated for repeated records, but other records are not repeated)
And the ID displays the maximum value. How can this problem be solved.
Generally used

Select Max (id) id, max (bookname)
Bookname, max (cbs) cbs, max (zz) zz, new_id from table group by new_id


The field followed by the group by clause is the condition for you to judge repetition. For example, if only col1 is used, if the content of col1 is the same, the record is the same.


Col1 + ',' + col2 + ','... col5 federated primary key

 

Select * from table where col1 + ',' + col2 + ','... col5 in (
Select max (col1 + ',' + col2 + ','... col5) from table
Where having count (*)> 1
Group by col1, col2, col3, col4
)


/////////

Select t1.bookname, t1.zz, t1.cbs from table t1
Join (select min (id) as id, bookname from table
Group by bookname) as t2
On t1.bookname = t2.bookname and t1.id = t2.id

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.