Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « T-SQL To find Out If An Index Is Clustered Or Non ClusteredSuffer to succeed. Another SQL Server Import Story »
    comments

    What is the percentage of null values in a table for a column? This question comes up every now and then and it is pretty easy to answer this question

    We will start by creating the following table

    1. CREATE TABLE #perc ( Column1 INT,Column2 INT,Column3 INT)
    2. INSERT INTO #perc
    3. SELECT NULL,1,1
    4. UNION ALL
    5. SELECT 1,1,1
    6. UNION ALL
    7. SELECT NULL,NULL,1
    8. UNION ALL
    9. SELECT NULL,1,NULL
    10. UNION ALL
    11. SELECT NULL,1,1
    12. UNION ALL
    13. SELECT 1,1,NULL
    14. UNION ALL
    15. SELECT NULL,1,1
    16. UNION ALL
    17. SELECT 2,1,2
    18. UNION ALL
    19. SELECT 3,1,1

    There are a couple of ways to calculate this but first we need to understand one thing: COUNT(*) and COUNT(ColumnName) behave differently, COUNT(*) will count NULLS while COUNT(ColumnName) does not!!!

    Here is one way to calculate the percentages, you use the following formula

    1. SUM(CASE WHEN ColumnName IS NULL THEN 1 ELSE 0 END) / COUNT(*)

    Run this query below

    1. SELECT 100.0 * SUM(CASE WHEN Column1 IS NULL THEN 1 ELSE 0 END) / COUNT(*) AS Column1Percent,
    2. 100.0 * SUM(CASE WHEN Column2 IS NULL THEN 1 ELSE 0 END) / COUNT(*) AS Column2Percent,
    3. 100.0 * SUM(CASE WHEN Column3 IS NULL THEN 1 ELSE 0 END) / COUNT(*) AS Column3Percent
    4. FROM #perc

    output

    Column1Percent	Column2Percent	Column3Percent
    55.555555555555	11.111111111111	22.222222222222

    Instead of using SUM and a CASE statement you can also just do the formula below to get the percentage of NULLS

    1. (COUNT(*) - COUNT(ColumnName)) / COUNT(*)

    The query below will return the same output as the query above

    1. SELECT 100.0 * (COUNT(*) - COUNT(Column1)) / COUNT(*) AS Column1Percent,
    2. 100.0 * (COUNT(*) - COUNT(Column2)) / COUNT(*) AS Column2Percent,
    3. 100.0 * (COUNT(*) - COUNT(Column3)) / COUNT(*) AS Column3Percent
    4. FROM #perc

    output

    Column1Percent	Column2Percent	Column3Percent
    55.555555555555	11.111111111111	22.222222222222

    What if you want to get a percentage of all values in the column? So for example we will take Column3 from the table

    1. select Column3 from #perc
    1
    1
    1
    NULL
    1
    NULL
    1
    2
    1

    As you can see we have
    2 rows with a value of NULL = 22.22% (select (2.0/9.0) * 100)
    1 row with a value of 2 = 11.11% (select (1.0/9.0) * 100
    6 rows with a value of 1 = 22.22% (select (6.0/9.0) * 100)

    Here is the query which accomplishes this requirement

    1. SELECT COALESCE(CONVERT(VARCHAR(50),Column3),'NULL') AS Value,
    2. COUNT(Column3) AS ValueCount,
    3. 100.0 * COUNT(*)/(SELECT COUNT(*) FROM #perc ) AS Percentage
    4. FROM #perc
    5. GROUP BY Column3
    6. ORDER BY Percentage DESC

    And here is the output
    Output

    Value	ValueCount	Percentage
    1	6		66.666666666666
    NULL	0		22.222222222222
    2	1		11.111111111111

    That is all, as you can see it is pretty trivial to calculate NULLS in a column




    *** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    5465 views
    how to, null, sql, tip
    Instapaper

    2 comments

    Comment from: Ted Krueger (onpnt) [Member]
    *****
    Ted Krueger (onpnt) Good stuff Denis. This got me more than a few times
    10/02/09 @ 08:36
    Comment from: Naomi Nosonovsky [Member]
    *****
    Naomi Nosonovsky Yes, here is a recent question for this problem
    http://social.msdn.microsoft.com/Forums/en-CA/transactsql/thread/f39a0907-18ba-49bf-b6c6-15e647e8a560
    10/04/09 @ 07:53

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)