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:
SQL COUNT (*) instance if we omit the WHERE clause, for example: SELECT COUNT (*) as numberoforders from the orders result set is similar to this:
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:
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