SQL to duplicate query

Source: Internet
Author: User
Tags ming

SQL Single or group de-duplication Query method

The following data are used for testing:

Table Name: T_demo

Id Name Project Grade
1 Xiao ming Mathematical 59
1 Xiao ming Chinese 59
1 Xiao ming English 59
2 Little Red Mathematical 100
2 Little Red Chinese 99
3 Small white Mathematical 100
3 Small white Chinese 90
3 Small white English 80
3 Small white Political 70
3 Small white History 60
4 Little Black Mathematical 89
4 Little Black Chinese 89
4 Little Black English 90

1. Single field to repeat query

We now want to get the ID field information table in the table above and remove the duplicate values, which means we now want to get the number of IDs in the table.

Select ID from T_demo GROUP by ID

Query Result:


Id
1
2
3
4


2. Multi-field to repeat the query

If you need to get the ID and name remove the duplicate value information table, the equivalent of getting the number of people in this table now.

Select Id,name from T_demo GROUP by Id,name

Query Result:


Id Name
1 Xiao ming
2 Little Red
3 Small white
4 Little Black


3. Multiple fields to repeat and get the number of duplicates

Now we need to get the number of everyone in the table.

Select Id,name,count (DISTINCT Project) as Num from T_demo GROUP by Id,name

Query Result:

Id
Name Num
1 Xiao ming 3
2 Little Red 2
3 Small white 5
4 Little Black 3


This article is from the "World is the same" blog, please be sure to keep this source http://970076933.blog.51cto.com/9767314/1890703

SQL to duplicate query

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.