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
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6335 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 Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 67
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
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19872 Visits: 5191
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
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2615 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
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

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

Group: General Forum Members
Points: 19872 Visits: 5191
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 Guru
SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)

Group: General Forum Members
Points: 92885 Visits: 9549
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
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 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
SSCertifiable
SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)SSCertifiable (6.1K reputation)

Group: General Forum Members
Points: 6102 Visits: 1858
...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
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27080 Visits: 7488
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; Theyll 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