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


Using a column name in a COUNT function


Using a column name in a COUNT function

Author
Message
Charles Kincaid
Charles Kincaid
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: 3161 Visits: 2384
Comments posted to this topic are about the item Using a column name in a COUNT function

ATBCharles Kincaid
roman.asadovsky
roman.asadovsky
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 58
That is well-known behavior of the COUNT(), but the question I have is this: What makes you think that COUNT(1) in any way superior to CONT(*)?
Igor Micev
Igor Micev
SSCrazy Eights
SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)

Group: General Forum Members
Points: 9936 Visits: 5155
Hi,

Why not use

select p.[rows] from sys.partitions p 
where p.index_id in (0,1) and p.object_id = object_id('[schema].[TableName]')


to replace count(1)?

You already have the count for every table in sys.partitions view.

Regards,
IgorMi

Igor Micev,
My blog: www.igormicev.com
Daniel Fountain
Daniel Fountain
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1343 Visits: 890
According to MS sys.partitions.rows "Indicates the approximate number of rows in this partition"

http://technet.microsoft.com/en-us/library/ms175012.aspx
Shahjahandurrani
Shahjahandurrani
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 48
Count(*) doesn't load the entire table. It uses the index to return count.
Igor Micev
Igor Micev
SSCrazy Eights
SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)

Group: General Forum Members
Points: 9936 Visits: 5155
danielfountain (10/15/2013)
According to MS sys.partitions.rows "Indicates the approximate number of rows in this partition"

http://technet.microsoft.com/en-us/library/ms175012.aspx


Hi,
Microsoft recommends to use the new dynamic views instead of some deprecated for future use
The same result can be obtained by using this dynamic view as well

select i.rowcnt from sys.sysindexes i where i.id = OBJECT_ID('[schema].[TableName]')
and i.indid = 1



I often use sys.partitions and it always gives out the same result as count(1). If the maintenance of the indexes is regularly done than that info is exact. However, a good remark of you, thanks.

Regards,
IgorMi

Igor Micev,
My blog: www.igormicev.com
Brandie Tarvin
Brandie Tarvin
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35519 Visits: 9257
The best way I've found for using COUNT(MyCol) is to use either the identity column or the primary key column. That way there are no NULL results to worry about.


SELECT COUNT(NameID) FROM CountTestSET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;

IF EXISTS (SELECT object_id
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[CountTest]')
AND type in (N'U')
)
DROP TABLE dbo.[CountTest];

CREATE TABLE dbo.[CountTest]([NameID] INT NOT NULL IDENTITY(1,1),
[Name] [nvarchar](max)
);

INSERT INTO dbo.[CountTest] ([Name]) VALUES('Sally');
INSERT INTO dbo.[CountTest] ([Name]) VALUES(NULL);
INSERT INTO dbo.[CountTest] ([Name]) VALUES('Mary');
INSERT INTO dbo.[CountTest] ([Name]) VALUES('Jane');
INSERT INTO dbo.[CountTest] ([Name]) VALUES(NULL);
INSERT INTO dbo.[CountTest] ([Name]) VALUES('Bob');
INSERT INTO dbo.[CountTest] ([Name]) VALUES('Tom');
INSERT INTO dbo.[CountTest] ([Name]) VALUES(NULL);

SELECT COUNT(NameID) FROM dbo.CountTest; --Gives count of 8
SELECT COUNT(Name) FROM dbo.CountTest; --Gives count of 5

DROP TABLE dbo.CountTest;




Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
djjunio
djjunio
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 20
hi, I usually have the following scenario: I have to count the distincts names

for this I use:

SELECT COUNT(distinct name) AS [COUNT distinct] FROM [CountTest];
Mighty
Mighty
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4949 Visits: 1750
...This can produce some surprising results. This is because of the way that COUNT() works...

Why the surprise if COUNT exactly does what is described in e.g. Technet?
MyDoggieJessie
MyDoggieJessie
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11696 Visits: 7442
You know that doing COUNT(*) on a table with a lot of columns and a lot of rows can take a lot of time and memory
There is virtually no difference between SELECT(*) and SELECT(1) - the execution plans are identical, and each will produce the same number of logical reads.

______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience" ;-)
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