Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

What's the count? Expand / Collapse
Author
Message
Posted Thursday, March 11, 2010 8:19 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, December 15, 2014 2:59 PM
Points: 908, Visits: 270
Nice one - thanks. I just "recovered" from a Cartesian Product, myself.
Post #881046
Posted Thursday, March 11, 2010 8:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, November 20, 2014 3:18 PM
Points: 27, Visits: 39
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
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 6:07 PM
Points: 1,380, Visits: 1,751
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 works
http://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_BIG
http://msdn.microsoft.com/en-us/library/ms175997.aspx -- Microsoft's documentation for COUNT

Feel free to write and submit a QotD based on this info.
Post #881102
Posted Thursday, March 11, 2010 10:47 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, November 24, 2014 8:33 AM
Points: 1,676, Visits: 1,760
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
Post #881190
Posted Thursday, March 11, 2010 1:55 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr 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

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

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


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:32 AM
Points: 3,552, Visits: 1,542
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

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr 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 Trast
Microsoft 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

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 6:55 AM
Points: 9,928, Visits: 11,203
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
Post #892802
Posted Wednesday, March 31, 2010 9:27 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 74, Visits: 297
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.
Post #893885
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse