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:
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