Sql-select count Usage

Source: Internet
Author: User
Tags microsoft sql server

SQL Server Database the Count () function returns the number of rows that match the specified criteria. The syntax SQL count (column_name) syntax count (column_name) function returns the number of values for the specified column (NULL does not count in): SELECT COUNT (column_name) from Table_namesql COUNT (*) syntax count (*) function returns the number of records in the table: SELECT count (*) from Table_namesql count (DISTINCT column_name) syntax count (DISTINCT c Olumn_name) function returns the number of different values for the specified column: SELECT count (DISTINCT column_name) from table_name Comment: COUNT (DISTINCT) for ORACLE and Microsoft SQL Server, but not for Microsoft Access.ExampleWe have the following "Orders" table:
o_id OrderDate Orderprice Customer
1 2008/12/29 1000 Bush
2 2008/11/23 1600 Carter
3 2008/10/05 700 Bush
4 2008/09/28 300 Bush
5 2008/08/06 2000 Adams
6 2008/07/21 100 Carter
Now, we want to calculate the number of orders for the customer "Carter". We use the following SQL statement: SELECT COUNT (Customer) as Customernilsen from Orderswhere customer= ' Carter ' The result of the SQL statement is 2 because customer Carter has a common 2 Orders:
Customernilsen
2
SQL COUNT (*) instance if we omit the WHERE clause, for example: SELECT COUNT (*) as numberoforders from the orders result set is similar to this:
Numberoforders
6
This is the total number of rows in the table.SQL Instancecount and DISTINCT are often used together in order to find out how many different data are in the table (as to what is actually unimportant). Now, we want to calculate the number of different customers in the Orders table. We use the following SQL statement: SELECT COUNT (DISTINCT Customer) as numberofcustomers from the orders result set is similar to this:
Numberofcustomers
3
This is the number of different customers (Bush, Carter, and Adams) in the Orders table. http://blog.csdn.net/kalision/article/details/7739190

Sql-select count Usage

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.