Usage of SQL Server database distinct

Source: Internet
Author: User

Distinct: Used to filter duplicate records. It is often used only to return the number of non-repeating records, rather than using it to return all values that are not re-recorded. The reason is that distinct only with double-loop query to solve, and so for a very large number of stations, will undoubtedly directly affect the efficiency.

Form Name table
Id NAME
1 A
2 B
3 B
4 C

1. Example: Query the name for all data that is not duplicated. Note: Distinct must be placed at the beginning

Select distinct  from Table
A
B
C

2. Query the name for all data that is not duplicated, including the ID.

Select distinct  from Table
1 A
2 B
3 B
4 C

So it's important to note that there are multiple fields behind the distinct, which are multi-conditional, and are duplicates only when these conditions are the same.

3. Want to get the following results how to write SQL

1 A
2 B
4 C
Select *   from Table Group  by name

Summarize:

Distinct is used to go heavy.

Must be placed in the first after the select

Multiple fields behind distinct are multi-conditional deduplication, which is the only way to duplicate records if these conditions are the same.

Usage of SQL Server database distinct

Related Article

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.