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
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

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

Group: General Forum Members
Points: 40 Visits: 57
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
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5834 Visits: 5080
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,
SQL Server developer at Seavus
My blog: www.igormicev.com
Daniel Fountain
Daniel Fountain
SSC Eights!
SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)SSC Eights! (965 reputation)

Group: General Forum Members
Points: 965 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
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

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

Group: General Forum Members
Points: 5834 Visits: 5080
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,
SQL Server developer at Seavus
My blog: www.igormicev.com
Brandie Tarvin
Brandie Tarvin
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14403 Visits: 8978
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 (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 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.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4403 Visits: 1697
...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
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

Group: General Forum Members
Points: 6646 Visits: 7391
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