Skip to content

Commit f0717e7

Browse files
authored
Merge pull request #36298 from rwestMSFT/rw-0119-fix-544814
Refresh COUNT and COUNT_BIG, and fix ALL syntax (UUF 544814)
2 parents 92e2f5f + cb87ba1 commit f0717e7

2 files changed

Lines changed: 366 additions & 138 deletions

File tree

docs/t-sql/functions/count-big-transact-sql.md

Lines changed: 264 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: "COUNT_BIG (Transact-SQL)"
33
description: "COUNT_BIG (Transact-SQL)"
44
author: markingmyname
55
ms.author: maghan
6-
ms.date: "07/24/2017"
6+
ms.reviewer: randolphwest
7+
ms.date: 01/19/2026
78
ms.service: sql
89
ms.subservice: t-sql
910
ms.topic: reference
@@ -23,59 +24,274 @@ dev_langs:
2324
monikerRange: ">=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric || =fabric-sqldb"
2425
---
2526
# COUNT_BIG (Transact-SQL)
27+
2628
[!INCLUDE [sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw-fabricsqldb](../../includes/applies-to-version/sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw-fabricsqldb.md)]
2729

28-
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.
31+
3032
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
31-
32-
## Syntax
33-
33+
34+
## Syntax
35+
36+
Aggregation function syntax:
37+
3438
```syntaxsql
39+
COUNT_BIG ( { [ [ ALL | DISTINCT ] expression ] | * } )
40+
```
41+
42+
Analytic function syntax:
43+
44+
```syntaxsql
45+
COUNT_BIG ( { [ ALL ] expression | * } ) OVER ( [ <partition_by_clause> ] )
46+
```
3547

36-
-- Aggregation Function Syntax
37-
COUNT_BIG ( { [ [ ALL | DISTINCT ] expression ] | * } )
38-
39-
-- Analytic Function Syntax
40-
COUNT_BIG ( [ ALL ] { expression | * } ) OVER ( [ <partition_by_clause> ] )
41-
```
42-
4348
## 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 &#40;Transact-SQL&#41;](../../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+
5970
## Return types
71+
6072
**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 &#40;Transact-SQL&#41;](../../t-sql/functions/count-transact-sql.md) for examples.
73-
74-
## See also
75-
[Aggregate Functions &#40;Transact-SQL&#41;](../../t-sql/functions/aggregate-functions-transact-sql.md)
76-
[COUNT &#40;Transact-SQL&#41;](../../t-sql/functions/count-transact-sql.md)
77-
[int, bigint, smallint, and tinyint &#40;Transact-SQL&#41;](../../t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql.md)
78-
[OVER Clause &#40;Transact-SQL&#41;](../../t-sql/queries/select-over-clause-transact-sql.md)
79-
80-
8173

74+
## Remarks
75+
76+
`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).
94+
95+
## Examples
96+
97+
[!INCLUDE [article-uses-adventureworks](../../includes/article-uses-adventureworks.md)]
98+
99+
### A. Use COUNT_BIG and DISTINCT
100+
101+
This example returns the number of different job titles in the `HumanResources.Employee` table that an employee can hold.
102+
103+
```sql
104+
SELECT COUNT_BIG(DISTINCT JobTitle)
105+
FROM HumanResources.Employee;
106+
GO
107+
```
108+
109+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
110+
111+
```output
112+
-----------
113+
67
114+
```
115+
116+
### B. Use COUNT_BIG(*)
117+
118+
This example returns the total number of employees in the `HumanResources.Employee` table.
119+
120+
```sql
121+
SELECT COUNT_BIG(*)
122+
FROM HumanResources.Employee;
123+
GO
124+
```
125+
126+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
127+
128+
```output
129+
-----------
130+
290
131+
```
132+
133+
### C. Use COUNT_BIG(*) with other aggregates
134+
135+
This example shows that `COUNT_BIG(*)` works with other aggregate functions in the `SELECT` list.
136+
137+
```sql
138+
SELECT COUNT_BIG(*), AVG(Bonus)
139+
FROM Sales.SalesPerson
140+
WHERE SalesQuota > 25000;
141+
GO
142+
```
143+
144+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
145+
146+
```output
147+
------ ---------------------
148+
14 3472.1428
149+
```
150+
151+
### D. Use the OVER clause
152+
153+
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 DISTINCT d.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
161+
FROM HumanResources.EmployeePayHistory AS eph
162+
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
163+
ON eph.BusinessEntityID = edh.BusinessEntityID
164+
INNER JOIN HumanResources.Department AS d
165+
ON d.DepartmentID = edh.DepartmentID
166+
WHERE edh.EndDate IS NULL
167+
ORDER BY d.Name;
168+
```
169+
170+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
171+
172+
```output
173+
Name MinSalary MaxSalary AvgSalary EmployeesPerDept
174+
---------------------------- ----------- ----------- ----------- -----------------
175+
Document Control 10.25 17.7885 14.3884 5
176+
Engineering 32.6923 63.4615 40.1442 6
177+
Executive 39.06 125.50 68.3034 4
178+
Facilities and Maintenance 9.25 24.0385 13.0316 7
179+
Finance 13.4615 43.2692 23.935 10
180+
Human Resources 13.9423 27.1394 18.0248 6
181+
Information Services 27.4038 50.4808 34.1586 10
182+
Marketing 13.4615 37.50 18.4318 11
183+
Production 6.50 84.1346 13.5537 195
184+
Production Control 8.62 24.5192 16.7746 8
185+
Purchasing 9.86 30.00 18.0202 14
186+
Quality Assurance 10.5769 28.8462 15.4647 6
187+
Research and Development 40.8654 50.4808 43.6731 4
188+
Sales 23.0769 72.1154 29.9719 18
189+
Shipping and Receiving 9.00 19.2308 10.8718 6
190+
Tool Design 8.62 29.8462 23.5054 6
191+
```
192+
193+
## Examples: Azure Synapse Analytics and Analytics Platform System (PDW)
194+
195+
### E. Use COUNT_BIG and DISTINCT
196+
197+
This example returns the number of different titles that an employee of a specific company can hold.
198+
199+
```sql
200+
USE ssawPDW;
201+
SELECT COUNT_BIG(DISTINCT Title)
202+
FROM dbo.DimEmployee;
203+
```
204+
205+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
206+
207+
```output
208+
-----------
209+
67
210+
```
211+
212+
### F. Use COUNT_BIG(*)
213+
214+
This example returns the total number of rows in the `dbo.DimEmployee` table.
215+
216+
```sql
217+
USE ssawPDW;
218+
SELECT COUNT_BIG(*)
219+
FROM dbo.DimEmployee;
220+
```
221+
222+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
223+
224+
```output
225+
-------------
226+
296
227+
```
228+
229+
### G. Use COUNT_BIG(*) with other aggregates
230+
231+
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.
232+
233+
```sql
234+
USE ssawPDW;
235+
SELECT COUNT_BIG(EmployeeKey) AS TotalCount,
236+
AVG(SalesAmountQuota) AS [Average Sales Quota]
237+
FROM dbo.FactSalesQuota
238+
WHERE SalesAmountQuota > 500000
239+
AND CalendarYear = 2001;
240+
```
241+
242+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
243+
244+
```output
245+
TotalCount Average Sales Quota
246+
---------- -------------------
247+
10 683800.0000
248+
```
249+
250+
### H. Use COUNT_BIG with HAVING
251+
252+
This example uses `COUNT_BIG` with the `HAVING` clause to return the departments of a company, each of which has more than 15 employees.
253+
254+
```sql
255+
USE ssawPDW;
256+
SELECT DepartmentName,
257+
COUNT_BIG(EmployeeKey) AS EmployeesInDept
258+
FROM dbo.DimEmployee
259+
GROUP BY DepartmentName
260+
HAVING COUNT_BIG(EmployeeKey) > 15;
261+
```
262+
263+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
264+
265+
```output
266+
DepartmentName EmployeesInDept
267+
-------------- ---------------
268+
Sales 18
269+
Production 179
270+
```
271+
272+
### I. Use COUNT_BIG with OVER
273+
274+
This example uses `COUNT_BIG` with the `OVER` clause, to return the number of products contained in each of the specified sales orders.
275+
276+
```sql
277+
USE ssawPDW;
278+
SELECT DISTINCT COUNT_BIG(ProductKey) OVER (PARTITION BY SalesOrderNumber) AS ProductCount,
279+
SalesOrderNumber
280+
FROM dbo.FactInternetSales
281+
WHERE SalesOrderNumber IN (N'SO53115', N'SO55981');
282+
```
283+
284+
[!INCLUDE [ssResult](../../includes/ssresult-md.md)]
285+
286+
```output
287+
ProductCount SalesOrderID
288+
------------ -----------------
289+
3 SO53115
290+
1 SO55981
291+
```
292+
293+
## Related content
294+
295+
- [Aggregate Functions (Transact-SQL)](aggregate-functions-transact-sql.md)
296+
- [COUNT (Transact-SQL)](count-transact-sql.md)
297+
- [int, bigint, smallint, and tinyint](../data-types/int-bigint-smallint-and-tinyint-transact-sql.md)

0 commit comments

Comments
 (0)