Log in  ::  Register  ::  Not logged in

## What's the count?

 Author Message cajun_sql Ten Centuries Group: General Forum Members Points: 1236 Visits: 400 Nice one - thanks. I just "recovered" from a Cartesian Product, myself. :-) wall str33t SSC-Enthusiastic Group: General Forum Members Points: 113 Visits: 45 A better question might be is there a difference between count(*) and count(col).Many people don't know the subtleties between them. sknox SSCertifiable Group: General Forum Members Points: 6933 Visits: 3159 wall str33t (3/11/2010)A better question might be is there a difference between count(*) and count(col).Many people don't know the subtleties between them.Rather than re-answer that question, I'll provide a link to where you can find that information:http://www.sqlservercentral.com/Forums/FindPost879366.aspx -- My post explaining how COUNT_BIG workshttp://www.sqlservercentral.com/Forums/FindPost879538.aspx -- Oleg's excellent test setup illustrating my explanation These are from the discussion of the March 9 QotD by agrawal.prakriti, about COUNT_BIG. The only difference between COUNT_BIG and COUNT is that COUNT_BIG returns a BIGINT rather than an INT. For official reference:http://msdn.microsoft.com/en-us/library/ms190317.aspx -- Microsoft's documentation for COUNT_BIGhttp://msdn.microsoft.com/en-us/library/ms175997.aspx -- Microsoft's documentation for COUNTFeel free to write and submit a QotD based on this info. Oleg Netchaev Hall of Fame Group: General Forum Members Points: 3076 Visits: 1910 Carlo Romagnano (3/11/2010)if one table has one o more rows and the other is void, the result is 0.NumberOfRows * 0 = 0This is a very good feature of the count function, to still return 0 if there is nothing to count. This makes it different from other aggregates, which will return null if there is nothing to consider. For example,use AdventureWorks;godeclare @one_or_more table ( DepartmentID smallint, [Name] nvarchar(50));declare @void table (DepartmentID smallint, [Name] nvarchar(50));-- insert 10 records into @one_or_more and leave @void emptyinsert into @one_or_more select top 10 DepartmentID, [Name] from HumanResources.Department;select count(*) RecordCount, min(A.DepartmentID) DepartmentID from @one_or_more A, @void B;goresulting inRecordCount DepartmentID----------- ------------0 NULLillustrates the point. The cartesian product does not return anything because the @void has no records, the min(A.DepartmentID) is therefore null though the value of the first department in the @one_or_more is actually 1, but the count shines here as it still returns 0, not null. I am not saying that implementation of the count is better, but I find this feature extremely useful.This is a very good question, really liked it.Oleg Steve Cullen SSCrazy Group: General Forum Members Points: 2749 Visits: 1226 My definition of a good QOD: Anytime I get the correct answer :-DGood Question! Converting oxygen into carbon dioxide, since 1955. sameerchachad 69959 Grasshopper Group: General Forum Members Points: 20 Visits: 41 Good question. This was really a mind teaser. Never thought of doing a COUNT for two tables. Scott Arendt SSCertifiable Group: General Forum Members Points: 5485 Visits: 1733 Well I learned something today. I had no idea you could produce a cross join in that manner. Peter Trast SSCommitted Group: General Forum Members Points: 1736 Visits: 655 Well, I guessed that it would cross join and got lucky Now I have a new way to do cross joins! Can't wait to kill my server with that one... Peter TrastMicrosoft Certified ...(insert many literal strings here)Microsoft Design Architect with Alexander Open Systems Paul White SSC Guru Group: General Forum Members Points: 79608 Visits: 11400 Amazing that so many people are unaware of the syntax. Especially since it pre-dates the CROSS JOIN form. Paul WhiteSQLPerformance.comSQLblog.com@SQL_Kiwi timothy.g.parker SSC Journeyman Group: General Forum Members Points: 92 Visits: 297 I tried the following statement on some existing tablesselect count(*) from table1, table2to verify the results of the question. As it turns out, with my data set, the query returns the "Arithmetic overflow error converting expression to data type int". It should be noted, that depending on your record sets, the count(*) should be replaced with count_big(*) to give the you the correct results.