The COUNT_BIG function performs the same function as the COUNT function. The only difference is that the COUNT_BIG function returns the BIGINT value while the COUNT function returns int value.
Let’s make examples for a better understanding of COUNT_BIG Function.
Example:
First, we create a table with the help of the following script and add a few records into this table.
1 2 3 4 5 6 7 8 9 10 11 12 | CREATE TABLE [dbo].[MyTable] ([ID] INT IDENTITY, [Name] VARCHAR(20), [Product] VARCHAR(20) ) INSERT [dbo].[MyTable] VALUES ('Hakan GURBASLAR','Product1'), ('Nurullah CAKIR','Product2'), ('Faruk ERDEM','Product3'), ('Nurullah CAKIR','Product4'), ('Nurullah CAKIR','Product5'), ('Hakan GURBASLAR','Product6') |
Let’s calculate the number of records in the table with one of the following queries: (Both queries return the same result. If you type a column name instead of 1 or *, null records are not calculated.)
1 | SELECT COUNT_BIG(1) FROM [dbo].[MyTable] |
or
1 | SELECT COUNT_BIG(*) FROM [dbo].[MyTable] |
Calculate how many products each person has by grouping table by [Name] column:
1 2 3 | Select [Name],COUNT_BIG(Product) AS Product_Count FROM [dbo].[MyTable] GROUP BY [Name] ORDER BY COUNT_BIG(Product) DESC |