Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

Tags: sqlserver

All the LessThanDot Journals

Getting The Percentage Of NULLS And Values In A SQL Server Table

by SQLDenis


Permalink 05 Aug 2008 08:09 , Categories: Data Modelling & Design Tags: database, math, null, sql, sqlserver

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
Leave a comment »Send a trackback » 660 views

Good SQL Articles To Read If You Can't Afford Books

by SQLDenis


Permalink 23 May 2008 09:11 , Categories: Data Modelling & Design, Microsoft SQL Server Admin, Microsoft SQL Server Tags: database, deadlocks, functions, howto, math, sql, sqlserver, tips, toread, tricks

You have only $50 left and you can buy two DVDs or one SQL book, what do you do? I would buy the book but not every person has the same idea of a fun time. This is the reason why I present you with a bunch of links to articles which will give you very good info. some of this you won’t be able to find in a book anyway.

The curse and blessings of dynamic SQL. How you use dynamic SQL, when you should - and when you should not.

Arrays and Lists in SQL Server. Several methods on how to pass an array of values from a client to SQL Server, and performance data about the methods. Two versions are available, one for SQL 2005 and one for SQL 2000 and earlier.

Implementing Error Handling with Stored Procedures and Error Handling in SQL Server – a Background. Two articles on error handling in SQL Server.

The ultimate guide to the datetime datatypes
The purpose of this article is to explain how the datetime datatypes work in SQL Server, including common pitfalls and general recommendations.

Stored procedure recompiles and SET options
Using stored procedures is generally considered a good thing. One advantage of stored procedures is that they are precompiled. This means that at execution time, SQL Server will fetch the precompiled procedure plan from cache memory (if exists) and execute it. This is generally faster than optimizing and compiling the code for each execution. However, under some circumstances, a procedure needs to be recompiled during execution.

Do You Know How Between Works With Dates?
article explaining why it can be dangerous to use between with datetime data types

How Are Dates Stored Internally In SQL Server?
Article explaining how datetimes are actually stored internally

Three part deadlock troubleshooting post, a must read if you want to understand how to resolve deadlocks.
Deadlock Troubleshooting, Part 1
Deadlock Troubleshooting, Part 2
Deadlock Troubleshooting, Part 3

SQL Server 2005 Whitepapers List
A list of 29 different SQL Server 2005 Whitepapers

Keep a check on your IDENTITY columns in SQL ServerThis article shows you how to keep an eye on your IDENTITY columns and find out before they run out of values, and fail with an arithmetic overflow error.

Character replacements in T-SQL
Quite often SQL programmers are left with the dirty job of working with badly formatted strings mostly generated from external sources. Typical examples are badly structured date values, social security numbers with misplaced hyphens, badly formatted phone numbers etc. When the data set if small, in many cases, one can easily fix by a one time cleanup code snippet, but for larger sets one will need more generalized routines.

15 comments »1 trackback » 4567 views

How to get the selectivity of an index

by SQLDenis


Permalink 20 May 2008 09:32 , Categories: Data Modelling & Design, Database Programming, Database Administration, Microsoft SQL Server Admin, Microsoft SQL Server Tags: database, indexes, optimizing, sql, sqlserver

The selectivity of an index is extremely important. If your index is not selective enough then the optimizer will simply have to do a scan. This is also a reason why creating an index on a gender column does not make a lot of sense.

First create this table

USE tempdb
go

CREATE TABLE TestCompositeIndex (State char(2),Zip Char(5))
INSERT TestCompositeIndex VALUES('NJ','08540')
INSERT TestCompositeIndex VALUES('NJ','08540')
INSERT TestCompositeIndex VALUES('NY','10028')
INSERT TestCompositeIndex VALUES('NY','10021')
INSERT TestCompositeIndex VALUES('NY','10021')
INSERT TestCompositeIndex VALUES('NY','10021')
INSERT TestCompositeIndex VALUES('NY','10001')
INSERT TestCompositeIndex VALUES('NJ','08536')
INSERT TestCompositeIndex VALUES('NJ','08540')

If you have a composite index (composite means the index contains more than one column) you need to run this code.

DECLARE @Count int

SELECT DISTINCT State, Zip
FROM TestCompositeIndex;

SET @Count = @@ROWCOUNT;



SELECT (@Count*1.0) / COUNT(*) AS IndexSelectivity,
COUNT(*)AS TotalCount,
@Count AS DistinctCount
FROM TestCompositeIndex;

Result
——–
IndexSelectivity TotalCount DistinctCount
.555555555555 9 5

If you have a one column index you can use this code

SELECT (COUNT(DISTINCT State)* 1.0) / COUNT(*) AS IndexSelectivity,
COUNT(*) AS TotalCount,
COUNT(DISTINCT State) AS DistinctCount
FROM TestCompositeIndex;

Result
——–
IndexSelectivity TotalCount DistinctCount
.222222222222 9 2

Leave a comment »Send a trackback » 197 views