|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Yesterday @ 8:00 AM
Points: 679,
Visits: 145
|
|
Nice one - thanks. I just "recovered" from a Cartesian Product, myself.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, May 06, 2011 9:33 AM
Points: 16,
Visits: 20
|
|
A better question might be is there a difference between count(*) and count(col). Many people don't know the subtleties between them.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 9:31 AM
Points: 1,041,
Visits: 1,356
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 10:53 AM
Points: 1,662,
Visits: 1,709
|
|
Carlo Romagnano (3/11/2010) if one table has one o more rows and the other is void, the result is 0. NumberOfRows * 0 = 0 This 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; go
declare @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 empty insert 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
|
|
|
|
|
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.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, April 11, 2011 3:52 AM
Points: 18,
Visits: 40
|
|
| Good question. This was really a mind teaser. Never thought of doing a COUNT for two tables.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 6:30 AM
Points: 3,047,
Visits: 1,257
|
|
| Well I learned something today. I had no idea you could produce a cross join in that manner.
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Friday, March 15, 2013 10:35 AM
Points: 594,
Visits: 654
|
|
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 Trast Microsoft Certified ...(insert many literal strings here) Microsoft Design Architect with Alexander Open Systems
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:03 AM
Points: 10,990,
Visits: 10,543
|
|
Amazing that so many people are unaware of the syntax. Especially since it pre-dates the CROSS JOIN form.
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, April 04, 2013 2:57 PM
Points: 74,
Visits: 249
|
|
I tried the following statement on some existing tables
select count(*) from table1, table2
to 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.
|
|
|
|