Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
Please help me..
i want columnwise rowcount(excluding null values) in single query..
see the below example:
table1:
------------------------------
col1  |   col2      |   col3
------------------------------
 1    |  null       |   test
 2    |  1w         |   test1
 null |  null       |   test3 

------------------------------

output will be:
----------------------
columnname | count
-----------------
col1       | 2
col2       | 1
col3       | 3
------------------


for above output i have tried below sql query:-

SQL
select a.name ,(select count(a.name) from users b where a.name is not null)
from syscolumns a where id=(select id from sysobjects where name='users')
group by a.name


but it is giving me all tables rowcount like-
col1 |3
col2 |3
col3 |3


:(


Thanks in advance...
Posted
Updated 23-May-12 9:44am
v2

Try this
SQL
SELECT COUNT(COL1) AS COL1 ,COUNT(COL2) AS COL2 ,COUNT(COL3) AS COL3 FROM USER 


No need to write is not null because count() function does not count the null value.
 
Share this answer
 
Comments
ssd_coolguy 24-May-12 8:22am    
hey thanks..
More about Count at MSDN: Count (Transact-SQL)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900