Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 What's the count? Rate Topic Display Mode Topic Options
Author
 Message
 Posted Thursday, March 11, 2010 8:19 AM
 SSC Eights! Group: General Forum Members Last Login: Tuesday, September 20, 2016 10:17 AM Points: 918, Visits: 399
 Nice one - thanks. I just "recovered" from a Cartesian Product, myself.
Post #881046
 Posted Thursday, March 11, 2010 8:40 AM
 SSC Rookie Group: General Forum Members Last Login: Friday, January 23, 2015 8:21 AM Points: 27, 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.
Post #881069
 Posted Thursday, March 11, 2010 9:01 AM
 SSCommitted Group: General Forum Members Last Login: Yesterday @ 6:38 PM Points: 1,825, Visits: 2,584
 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.
Post #881102
 Posted Thursday, March 11, 2010 10:47 AM
 SSCommitted Group: General Forum Members Last Login: Friday, September 16, 2016 5:02 AM Points: 1,683, Visits: 1,791
 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;go`resulting in`RecordCount DepartmentID----------- ------------0 NULL`illustrates 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
Post #881190
 Posted Thursday, March 11, 2010 1:55 PM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Wednesday, September 19, 2012 8:39 AM Points: 595, Visits: 1,226
 My definition of a good QOD: Anytime I get the correct answer Good Question! Converting oxygen into carbon dioxide, since 1955.
Post #881326
 Posted Friday, March 12, 2010 1:27 AM
 Grasshopper Group: General Forum Members Last Login: Friday, July 26, 2013 2:49 AM Points: 18, Visits: 41
 Good question. This was really a mind teaser. Never thought of doing a COUNT for two tables.
Post #881559
 Posted Friday, March 12, 2010 6:45 AM
 SSCarpal Tunnel Group: General Forum Members Last Login: Wednesday, May 25, 2016 6:36 AM Points: 4,030, Visits: 1,702
 Well I learned something today. I had no idea you could produce a cross join in that manner.
Post #881713
 Posted Monday, March 15, 2010 11:19 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Friday, June 13, 2014 3:03 PM Points: 594, 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
Post #883168
 Posted Tuesday, March 30, 2010 8:12 AM
 SSCrazy Eights Group: General Forum Members Last Login: Friday, September 9, 2016 3:27 PM Points: 9,932, Visits: 11,343
 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
Post #892802
 Posted Wednesday, March 31, 2010 9:27 AM
 Valued Member Group: General Forum Members Last Login: Wednesday, December 17, 2014 1:04 PM Points: 74, 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.
Post #893885

 Permissions