Not getting the correct score in SQL

I am completely new to SQL . I have a simple selection request like this:

 SELECT COUNT(col1) FROM table1 

The table contains about 120 entries and is shown on the GUI . For some reason, this query always returns a number that is less than the actual invoice.

Can someone please help me?

+4
sql tsql sql-server-2005
04 Oct '08 at 5:17
source share
3 answers

Column col1 may have some null values. Aggregate functions ignore zeros. try it

 SELECT COUNT(ISNULL(col1,0)) FROM table1 
+13
04 Oct '08 at 5:18
source share

Try

 select count(*) from table1 

Edit: To explain further, count(*) gives you a row for the table, including duplicates and zeros. count(isnull(col1,0)) will do the same, but a bit slower, since isnull must be evaluated for each row.

+17
04 Oct '08 at 5:18
source share

Slightly tangential, but also useful there.

 SELECT count(distinct cola) from table1 

which gives you the number of columns in the table.

+1
Oct. 06 '08 at 20:23
source share



All Articles