How to use SQL select distinct, selectdistinct
Duplicate values may exist in the table. This is not a problem. However, sometimes you may want to list only different values. The keyword distinct is used to return a unique value.
Table:
Example 1
Copy codeThe Code is as follows:
Select distinct name from
The result is as follows:
Example 2
Select distinct name, id from
The result is as follows:
Distinct serves both the name and id. This method is supported by both Access and SQL Server.
Example 3: Statistics
Select count (distinct name) from A; -- number of names deduplicated In the table, which is supported by SQL Server, but not by Access
Select count (distinct name, id) from A; -- neither SQL Server nor Access is supported.
Example 4
Select id, distinct name from A; -- an error is prompted because distinct must start
Others
In the distinct statement, the select field can only be a field specified by distinct, and other fields cannot appear. For example, if Table A has A "Remarks" column, it is impossible to directly use distinct to obtain the distinc name and the corresponding "Remarks" field.
However, you can use other methods to discuss how SQL Server concatenates multiple rows in a column into one row.