Description of how to delete duplicate records of redundant data in SQL statements. Only one record is retained.

Source: Internet
Author: User

Let's take a look at the relevant data structure knowledge.

When learning linear tables, there was such an example.

It is known that an ordered table named La stores integers. We try to construct an ordered table LB, which requires that the ordered table LB only contain data elements with different values in the ordered table LA.
AlgorithmIdeas:
First, the first element of the Order table la is paid to the LB of the Order table. Then, starting from the 2nd elements of the Order table la, each element is compared with each element in the order table lb, if they are different, the element is appended to the end of the LB sequence table.CopyCodeThe Code is as follows: public seqlist <int> purge (seqlist <int> La)
{
Seqlist <int> lB = new seqlist <int> (LA. maxsize );
// Assign the 1st data elements in Table A to table B
LB. append (La [0]);
// Process data elements in Table A in sequence
For (INT I = 1; I <= La. getlength ()-1; ++ I)
{
Int J = 0;
// Check whether table B has the same data element as Table.
For (j = 0; j <= LB. getlength ()-1; ++ J)
{
// Has the same data element
If (La [I]. compareto (Lb [J]) = 0)
{
Break;
}
}
// If no data element exists, append the data element in Table A to the end of Table B.
If (j> lb. getlength ()-1)
{
LB. append (La [I]);
}
Return Lb;
}
}

If you understand this idea, the processing in the database will be easy.

We can create a temporary table to solve the problem.Copy codeThe Code is as follows: Select distinct * into # TMP from tablename
Drop table tablename
Select * into tablename from # TMP
Drop table # TMP

The reason for this repetition is that the table design is not weekly. You can add a unique index column.

But you said, I don't want to add any fields, but there is no explicit ID column at this time. How can I retrieve the ID column? (It can be an Sn column, guid, etc)

Let's not talk about the last question. Let's take a look at it first.

Let's take a look at the solution in three databases: sqlserver2000, sqlserver2005, and Oracle 10 Gb.

1. SQL Server 2000 constructs the sequence number column

Method 1:
Select No. =
(Select count (customer number) from customer as a where a. Customer number <= B. Customer number ),
Customer ID, company name from customer as B order by 1;
Method 2:

Select No. = count (*),
A. Customer ID, A. company name from customer as a, customer as B
Where a. Customer No.> = B. Customer No. Group by A. Customer No., B. company name order by no;
2. SQL Server 2005 constructs the sequence number column

Method 1:
Select rank () over (order by customer No. DESC) as No., customer No., company name from customer;

Method 2:
With table
(Select row_number () over (order by customer No. DESC) as No., customer No., company name from customer)
Select * from table
Where no. Between 1 and 3;
3. rowid in Oracle can also be seen as the default identifier Column
In Oracle, each record has a rowid, which is unique throughout the database, rowid determines which data file, block, and row of each record in Oracle.
In a duplicate record, the content of all columns may be the same, but the rowid may not be the same. Therefore, you only need to determine those with the largest rowid in the record, and delete all the others. Copy code The Code is as follows: Select * from test; select * from test group by ID having count (*)> 1 select * from test group by idselect distinct * From testdelete from test a where. rowid! = (Select max (rowid) from Test B where. id = B. back to the original problem. Besides using the data structure, the data can be cached in the thread pool because of the unique transaction processing of the database, this is also equivalent to the temporary table function. Therefore, we can use a cursor to delete duplicate records.
Declare @ Max int,
@ ID int
Declare cur_rows cursor local for select ID, count (*) from test group by ID having count (*)> 1
Open cur_rows
Fetch cur_rows into @ ID, @ Max
While @ fetch_status = 0
Begin
Select @ max = @ max-1
Set rowcount @ max -- let the number of rows at this time be equal to the number of statistics for a row.
Delete from test where id = @ ID
Fetch cur_rows into @ ID, @ Max
End
Close cur_rows
Set rowcount 0 or above is the idea of lightning reading some materials. If you have any questions, please note.

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.