You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This function returns the number of items found in a group. `COUNT_BIG` operates like the [COUNT](../../t-sql/functions/count-transact-sql.md) function. These functions differ only in the data types of their return values. `COUNT_BIG` always returns a **bigint** data type value. `COUNT` always returns an **int** data type value.
29
-
30
+
This function returns the number of items found in a group. `COUNT_BIG` operates like the [COUNT](count-transact-sql.md) function. These functions differ only in the data types of their return values. `COUNT_BIG` always returns a **bigint** data type value. `COUNT` always returns an **int** data type value.
COUNT_BIG ( [ ALL ] { expression | * } ) OVER ( [ <partition_by_clause> ] )
41
-
```
42
-
43
48
## Arguments
44
-
ALL
45
-
Applies the aggregate function to all values. ALL serves as the default.
46
-
47
-
DISTINCT
48
-
Specifies that `COUNT_BIG` returns the number of unique nonnull values.
49
-
50
-
*expression*
51
-
An [expression](../../t-sql/language-elements/expressions-transact-sql.md) of any type. `COUNT_BIG` does not support aggregate functions or subqueries in an expression.
52
-
53
-
*\**
54
-
Specifies that `COUNT_BIG` should count all rows to determine the total table row count to return. `COUNT_BIG(*)` takes no parameters and does not support the use of DISTINCT. `COUNT_BIG(*)` does not require an *expression* parameter because by definition, it does not use information about any particular column. `COUNT_BIG(*)` returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately, including rows that contain null values.
55
-
56
-
OVER **(**[*partition_by_clause*][*order_by_clause*]**)**
57
-
The *partition_by_clause* divides the result set produced by the `FROM` clause into partitions to which the `COUNT_BIG` function is applied. If not specified, the function treats all rows of the query result set as a single group. The *order_by_clause* determines the logical order of the operation. See [OVER Clause (Transact-SQL)](../../t-sql/queries/select-over-clause-transact-sql.md) for more information.
58
-
49
+
50
+
#### ALL
51
+
52
+
Applies the aggregate function to all values. `ALL` serves as the default.
53
+
54
+
#### DISTINCT
55
+
56
+
Specifies that `COUNT_BIG` returns the number of unique non-null values.
57
+
58
+
#### *expression*
59
+
60
+
An [expression](../language-elements/expressions-transact-sql.md) of any type. `COUNT_BIG` doesn't support aggregate functions or subqueries in an expression.
61
+
62
+
#### \*
63
+
64
+
Specifies that `COUNT_BIG` should count all rows to determine the total table row count to return. `COUNT_BIG(*)` takes no parameters and doesn't support the use of `DISTINCT`. `COUNT_BIG(*)` doesn't require an *expression* parameter because by definition, it doesn't use information about any particular column. `COUNT_BIG(*)` returns the number of rows in a specified table, and it preserves duplicate rows. It counts each row separately, including rows that contain null values.
65
+
66
+
#### OVER ( [*partition_by_clause*][*order_by_clause*] )
67
+
68
+
The *partition_by_clause* divides the result set produced by the `FROM` clause into partitions to which the `COUNT_BIG` function is applied. If you don't specify the *partition_by_clause*, the function treats all rows of the query result set as a single group. The *order_by_clause* determines the logical order of the operation. For more information, see [OVER clause](../queries/select-over-clause-transact-sql.md).
69
+
59
70
## Return types
71
+
60
72
**bigint**
61
-
62
-
## Remarks
63
-
COUNT_BIG(\*) returns the number of items in a group. This includes NULL values and duplicates.
64
-
65
-
COUNT_BIG (ALL *expression*) evaluates *expression* for each row in a group, and returns the number of nonnull values.
66
-
67
-
COUNT_BIG (DISTINCT *expression*) evaluates *expression* for each row in a group, and returns the number of unique, nonnull values.
68
-
69
-
COUNT_BIG is a deterministic function when used **_without_** the OVER and ORDER BY clauses. COUNT_BIG is nondeterministic when used **_with_** the OVER and ORDER BY clauses. See [Deterministic and Nondeterministic Functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md) for more information.
70
-
71
-
## Examples
72
-
See [COUNT (Transact-SQL)](../../t-sql/functions/count-transact-sql.md) for examples.
`COUNT_BIG(*)` returns the number of items in a group. This count includes `NULL` values and duplicates.
77
+
78
+
`COUNT_BIG(ALL <expression>)` evaluates *expression* for each row in a group, and returns the number of non-null values.
79
+
80
+
`COUNT_BIG(DISTINCT <expression>)` evaluates *expression* for each row in a group, and returns the number of unique, non-null values.
81
+
82
+
### Deterministic and nondeterministic usage
83
+
84
+
`COUNT_BIG` is a *deterministic* function when used *without* the `OVER` and `ORDER BY` clauses.
85
+
86
+
`COUNT_BIG` is *nondeterministic* when used *with* the `OVER` and `ORDER BY` clauses.
87
+
88
+
| Uses `OVER` and `ORDER BY` clauses | Deterministic |
89
+
| --- | --- |
90
+
| No | Yes |
91
+
| Yes | No |
92
+
93
+
For more information, see [Deterministic and nondeterministic functions](../../relational-databases/user-defined-functions/deterministic-and-nondeterministic-functions.md).
This example uses the `MIN`, `MAX`, `AVG`, and `COUNT_BIG` functions with the `OVER` clause to return aggregated values for each department in the `HumanResources.Department` table.
154
+
155
+
```sql
156
+
SELECT DISTINCTd.Name,
157
+
MIN(eph.Rate) OVER (PARTITION BY edh.DepartmentID) AS MinSalary,
158
+
MAX(eph.Rate) OVER (PARTITION BY edh.DepartmentID) AS MaxSalary,
159
+
AVG(eph.Rate) OVER (PARTITION BY edh.DepartmentID) AS AvgSalary,
160
+
COUNT_BIG(edh.BusinessEntityID) OVER (PARTITION BY edh.DepartmentID) AS EmployeesPerDept
This example combines `COUNT_BIG(*)` with other aggregate functions in the `SELECT` list. It returns the number of sales representatives with an annual sales quota greater than $500,000, and the average sales quota of those sales representatives.
0 commit comments