Click here to Skip to main content
15,903,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,

I am stuck at a problem, not sure on how to go about writing a query that will return as a percentage the number of fields in a row that are null on the base of a user's id.

For instance, a row from my table:
Row1 : field1 field2 field3

If field3 is empty or null, my query should return 67%.

So far I have gotten the number of fields:
select count(1) from information_schema.columns where table_name='myTable'
I am using MS SQL 2008.
Thanks in avance friends
Posted
Updated 5-Jul-11 3:09am
v2

1 solution

Try:
SQL
SELECT 
   (SELECT (COUNT(1)*100) from myTable where ISNULL(myColumn,'')='') 
   / 
   (SELECT COUNT(1) FROM myTable)




"Thanks sir for your reply, but i need to count all the columns(columns having value) of a single row and show it in %"

Your original question asked for:
"return as a percentage the number of fields in a row that are null"

If you have 6 rows, with null in two of them, the query above will return "33" - ie, 33% or 1/3
To invert it, and return the percentage that are not null, put a "NOT" in front of the ISNULL:
SQL
SELECT 
   (SELECT (COUNT(1)*100) from myTable where NOT ISNULL(myColumn,'')='') 
   / 
   (SELECT COUNT(1) FROM myTable)
 
Share this answer
 
v2
Comments
pawanvats 5-Jul-11 9:15am    
Thanks sir for your reply, but i need to count all the columns(columns having value) of a single row and show it in %
OriginalGriff 5-Jul-11 10:13am    
Answer updated

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