How to remove Repeated Records in SQL statements (Review of knowledge points during overtime work)

Source: Internet
Author: User
I always thought that I don't need to work overtime today ....... the Support Training Department has completed data import to relieve the pressure on customer service mm (customer service mm: Please, please. If you cannot do it again, I will leave the phone overwhelmed on Monday. ...... Received to ensure the task is completed.) A table has been imported four times and three times have errors, furious ~~~~~~~~~~~~~~~~~~~~~ Duplicate data cannot be imported normally ~ (Around the world) developers are all happy weekend ~~~~ Must be operated independently ~ After a friend's help and experiment, I finally succeeded... The teacher taught me before. Forgot .. There are two Repeated Records. One is a completely repeated record, that is, records with all fields being repeated, and the other is records with duplicate key fields, such as duplicate name fields, other fields are not necessarily repeated or can be ignored. 1. For the first type of repeat, it is easy to solve. You can use select distinct * From tablename to obtain the result set without repeated records. If the table needs to delete duplicate records (one record is retained ), you can delete select distinct * into # TMP from tablename drop table tablename select * into tablename from # TMP drop table # TMP this duplication occurs because the table is designed for weeks, you can add a unique index column. 2. This type of repetition problem usually requires that the first record in the repeat record be retained. The operation method is as follows, assuming that there are repeated fields: name, address, select Identity (INT, 1, 1) as autoid, * into # TMP from tablename select Min (autoid) as autoid into # tmp2 from # TMP group by name, autoid select * from # TMP where autoid in (select autoid from # tmp2) The last select gets the name, the address does not repeat the result set (but an autoid field is added, which can be omitted in the select clause when writing)

 

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.