SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


The COUNT Function in T-SQL


The COUNT Function in T-SQL

Author
Message
Igor Micev
Igor Micev
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21171 Visits: 5206
Comments posted to this topic are about the item The COUNT Function in T-SQL

Igor Micev,
My blog: www.igormicev.com
NParry
NParry
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 134
Just tried these out, and this:

/*(9)*/ SELECT DISTINCT(COUNT(1)) FROM #TmpCounts; -- 1 row

Actually returns 6. This is on SQL Server 2008.
Igor Micev
Igor Micev
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21171 Visits: 5206
NParry (7/12/2016)
Just tried these out, and this:

/*(9)*/ SELECT DISTINCT(COUNT(1)) FROM #TmpCounts; -- 1 row

Actually returns 6. This is on SQL Server 2008.


6 is the result from the COUNT(1) and then DISTINCT(). Only 1 row is returned with a value 6.

Igor Micev,
My blog: www.igormicev.com
pmadhavapeddi22
pmadhavapeddi22
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3240 Visits: 1874
/*(1)*/ SELECT COUNT(*) FROM #TmpCounts; -- 6 rows
/*(2)*/ SELECT COUNT(1) FROM #TmpCounts; -- 6 rows
/*(3)*/ SELECT COUNT(Column1) FROM #TmpCounts; -- 5 rows
/*(4)*/ SELECT COUNT(ALL Column1) FROM #TmpCounts; -- 5 rows
/*(5)*/ SELECT COUNT(DISTINCT 1) FROM #TmpCounts; -- 1 row
/*(6)*/ SELECT COUNT(DISTINCT Column1) FROM #TmpCounts; -- 4 rows
/*(7)*/ SELECT COUNT('T') FROM #TmpCounts; --6 rows
/*(8)*/ SELECT COUNT(CONVERT(int, NULL)) FROM #TmpCounts; -- 0 rows
/*(9)*/ SELECT DISTINCT(COUNT(1)) FROM #TmpCounts; -- 1 row


for all of the above sql statement one row is returned with a value.. count returns single row value.. i am not sure why "rows" are mentioned in the comments.
Igor Micev
Igor Micev
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21171 Visits: 5206
pmadhavapeddi22 (7/12/2016)
/*(1)*/ SELECT COUNT(*) FROM #TmpCounts; -- 6 rows
/*(2)*/ SELECT COUNT(1) FROM #TmpCounts; -- 6 rows
/*(3)*/ SELECT COUNT(Column1) FROM #TmpCounts; -- 5 rows
/*(4)*/ SELECT COUNT(ALL Column1) FROM #TmpCounts; -- 5 rows
/*(5)*/ SELECT COUNT(DISTINCT 1) FROM #TmpCounts; -- 1 row
/*(6)*/ SELECT COUNT(DISTINCT Column1) FROM #TmpCounts; -- 4 rows
/*(7)*/ SELECT COUNT('T') FROM #TmpCounts; --6 rows
/*(8)*/ SELECT COUNT(CONVERT(int, NULL)) FROM #TmpCounts; -- 0 rows
/*(9)*/ SELECT DISTINCT(COUNT(1)) FROM #TmpCounts; -- 1 row


for all of the above sql statement one row is returned with a value.. count returns single row value.. i am not sure why "rows" are mentioned in the comments.

Yes. In the comments it should write "rows counted".
Thanks for the remark!

Igor Micev,
My blog: www.igormicev.com
Karl Klingler
Karl Klingler
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3410 Visits: 2142
Hello Igor,

in the article you pose the question "Which Is Faster: COUNT(*) or COUNT(1)?", but actually you answer with "COUNT(*) versus COUNT(ID)" (which is not the same as count(1))

Is there something missing in the article or did you just get the same results for COUNT(1) and COUNT(ID)?

regards
karl

Best regards
karl
Igor Micev
Igor Micev
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21171 Visits: 5206
Karl Klingler (7/12/2016)
Hello Igor,

in the article you pose the question "Which Is Faster: COUNT(*) or COUNT(1)?", but actually you answer with "COUNT(*) versus COUNT(ID)" (which is not the same as count(1))

Is there something missing in the article or did you just get the same results for COUNT(1) and COUNT(ID)?

regards
karl

Hello Karl,
Your remark is in place. Thanks.
Actually it's the same, because ID is the PK of the demo table. COUNT(1) and COUNT(ID) perform the same here. I'll consider this to correct in the text.

Igor Micev,
My blog: www.igormicev.com
patrickmcginnis59 10839
patrickmcginnis59 10839
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10463 Visits: 6868

An ORDER BY clause can be used explicitly in the end as well. In that case the COUNT is not deterministic. See Deterministic and Nondeterministic Functions for more.


but the page you referenced has


For example, specifying an ORDER BY clause in a query does not change the determinism of a function that used in that query.


Just wondering about your description of deterministic, it doesn't seem to make sense.

edit:
another snippet from the web page might shed some light on this:


Functions that call extended stored procedures are nondeterministic, because the extended stored procedures can cause side effects on the database.


I think a simple (maybe naïve) definition of "deterministic" could be that given the same input, the function will return the same output, but your writeup doesn't even match this. Heck strictly speaking, count(<whatever>Wink isn't even behaving as a function and I wouldn't even call it one. Maybe call it an aggregate or something catchy like that.

to properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Igor Micev
Igor Micev
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21171 Visits: 5206
patrickmcginnis59 10839 (7/12/2016)

An ORDER BY clause can be used explicitly in the end as well. In that case the COUNT is not deterministic. See Deterministic and Nondeterministic Functions for more.


but the page you referenced has


For example, specifying an ORDER BY clause in a query does not change the determinism of a function that used in that query.


Just wondering about your description of deterministic, it doesn't seem to make sense.

edit:
another snippet from the web page might shed some light on this:


Functions that call extended stored procedures are nondeterministic, because the extended stored procedures can cause side effects on the database.


I think a simple (maybe naïve) definition of "deterministic" could be that given the same input, the function will return the same output, but your writeup doesn't even match this. Heck strictly speaking, count(<whatever>Wink isn't even behaving as a function and I wouldn't even call it one. Maybe call it an aggregate or something catchy like that.


Please read carefully the reference. The aggregate functions are not deterministic when used with Over and Order By.

Igor Micev,
My blog: www.igormicev.com
John Bigler
John Bigler
SSC Veteran
SSC Veteran (237 reputation)SSC Veteran (237 reputation)SSC Veteran (237 reputation)SSC Veteran (237 reputation)SSC Veteran (237 reputation)SSC Veteran (237 reputation)SSC Veteran (237 reputation)SSC Veteran (237 reputation)

Group: General Forum Members
Points: 237 Visits: 113
Hi Igor,

In the What Index is Used When Using COUNT? section of the article, you write, "If the table has no indexes (hash) ...". I believe that should be a table heap, and not the word hash.

Regards,

John B.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search