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 12345»»»

Using a column name in a COUNT function Expand / Collapse
Author
Message
Posted Monday, October 14, 2013 9:29 PM


SSC Eights!

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

Group: General Forum Members
Last Login: Thursday, February 6, 2014 12:59 PM
Points: 801, Visits: 1,962
Comments posted to this topic are about the item Using a column name in a COUNT function

ATB

Charles Kincaid

Post #1504596
Posted Monday, October 14, 2013 11:32 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 21, 2014 8:25 AM
Points: 17, Visits: 37
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(*)?
Post #1504605
Posted Tuesday, October 15, 2013 1:11 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 11:12 AM
Points: 2,934, Visits: 2,959
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
www.seavus.com
Post #1504623
Posted Tuesday, October 15, 2013 1:55 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 2:24 AM
Points: 766, Visits: 841
According to MS sys.partitions.rows "Indicates the approximate number of rows in this partition"

http://technet.microsoft.com/en-us/library/ms175012.aspx
Post #1504633
Posted Tuesday, October 15, 2013 2:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, October 18, 2013 12:36 AM
Points: 2, Visits: 48
Count(*) doesn't load the entire table. It uses the index to return count.
Post #1504648
Posted Tuesday, October 15, 2013 2:47 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 11:12 AM
Points: 2,934, Visits: 2,959
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
www.seavus.com
Post #1504651
Posted Tuesday, October 15, 2013 6:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:35 AM
Points: 7,197, Visits: 6,342
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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #1504711
Posted Tuesday, October 15, 2013 6:43 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 28, 2013 9:41 AM
Points: 1, Visits: 14
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];

Post #1504718
Posted Tuesday, October 15, 2013 7:01 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: Friday, August 29, 2014 2:13 AM
Points: 3,246, Visits: 1,267
...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?
Post #1504732
Posted Tuesday, October 15, 2013 7:04 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: Wednesday, August 20, 2014 9:31 AM
Points: 3,910, Visits: 7,138
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"
Post #1504735
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse