How to use SQL SELECT DISTINCT _ database other

Source: Internet
Author: User
Tags how to use sql

In a table, you might include duplicate values. This is not a problem, but sometimes you may want to list only the different values (distinct). Keyword DISTINCT is used to return only a different value.

Table A:

Example 1

Copy Code code as follows:

Select distinct name from A

The results of the implementation are as follows:

Example 2
Select DISTINCT name, ID from A

The results of the implementation are as follows:

In fact, it is based on "Name+id", and distinct is also on the name and ID, which is supported both by Access and SQL Server.

Example 3: Statistics

Select COUNT (distinct name) from A; --The number of name resets in the table, SQL Server support, and access does not support
Select COUNT (distinct name, id) from A; Both--sql server and access do not support

Example 4

Select ID, distinct name from A; -the error is prompted because the distinct must be placed at the beginning

Other

The field in the distinct statement that the select displays can only be fields specified by distinct, and other fields are not available. For example, if Table A has a memo column, it is impossible to get distinc name and the corresponding "Memo" field directly through distinct.
However, there are other ways to discuss the issue of how SQL Server can concatenate a column of rows into one row

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.