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

« SQL Server 2008 cannot be installed if you have Visual Studio 2008 RTM installedHow Are Dates Stored In SQL Server? »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Sometimes you want to know what the percentage is of null values in a table for a Column
Or you might want to know what the percentage of all values in a Column is grouped by value
You can get these answers by running the code below

First create this 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

Get the percentage of nulls in all the Columns in my table

  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

Get the values and the percentage of all values for a Column

  1. SELECT Column3 AS VALUE,COUNT(*) AS ValueCount,
  2. 100.0 * COUNT(COALESCE(Column3,0))/(SELECT COUNT(*) FROM #perc ) AS Percentage
  3. FROM #perc
  4. GROUP BY Column3
  5. ORDER BY Percentage DESC

Output

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

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 SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
1737 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

No feedback yet

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.)