SQL de-weight

Source: Internet
Author: User

Distinct: Used to return only a different value.

Note: 1. The Select field in the statement can only be the field specified by distinct, and the other fields cannot be displayed.

2. Distinct must be placed at the beginning such as select distinct name from table

GROUP BY: "Grouping (group) according to certain rules (by)"

Its function is to divide a data set into several small areas by certain rules and then data processing for several small regions.
Note: 1. GROUP by the specified column, which must contain a clustered function such as (AVG, MAX, MIN, SUM, COUNT) in the query result

2, group by IS first sorted after grouping;

Form Name table

ID Name Address Telephone

1 Zhang three Harbin 123456789

2 John Doe Shanghai 784512357

3 Harry Beijing 759878456

4 Zhang San Hong Kong 785641231

ID unique, remove name duplicate records. Reserved with the largest ID, the following table is generated:

ID Name Address Telephone

2 John Doe Shanghai 784512357

3 Harry Beijing 759878456

4 Zhang San Hong Kong 785641231

You need to display extra fields at this time so you can't use the DISTINCT method!!

Correct wording: Based on a single field (Name), only the record with the highest ID is retained, and then filtered

Select*from table where ID in

(select Max (ID) from table group by Name has count (Name) >0)

Incorrect notation: GROUP by has actually grouped name,address,telephone simultaneously

Select Max (ID), name,address,telephone from table group by Name,address,telephone

Show Results:

ID Name Address Telephone

1 Zhang three Harbin 123456789

2 John Doe Shanghai 784512357

3 Harry Beijing 759878456

4 Zhang San Hong Kong 785641231

SQL de-weight

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.