Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 16th, 2005, 08:45 PM
Registered User
 
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Profiling data values in SQL Server fields

Hi,

I'm trying to figure out the best way to generate a t-sql query that will return a result set of the columns in a table along with each distinct value and a rowcount of each distinct value.

Example of what i'm looking for.

--
Movies table has the following columns:

movieid, title, movierating, genre, director


I started out with this:


--
select MovieRating, count(MovieRating) as total
from dbo.Movies
group by MovieRating
order by total desc
--

...and get the following result set:

MovieRating total
------------------ -----------
R 2100
PG 9050
PG-13 7000
Unrated 675
G 2975
NC-17 107

-----

That's nice but what i'm trying to get in one script is:

column_name distinct_value total
------------- ---------------- --------
movierating R 2100
movierating PG 9050
movierating PG-13 7000
movierating Unrated 675
.
.
.
.
title Jaws 1
title Star Wars I 1
title Star Trek 4 3
.
.
.
.
genre Comedy 3005
genre Adventure 1432
genre Drama 2500
.
.
.........etc.

Any ideas would be appreciated. Thanks.
 
Old February 18th, 2005, 01:36 AM
Authorized User
 
Join Date: Feb 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Solution to your problem:

-------------------------------------------------------------------------
CREATE PROCEDURE getColumnsDistinctValueCount
@TableName VARCHAR(255)
AS
BEGIN

    DECLARE @sqlString VARCHAR(8000)

    /*Building required dynamic SQL string*/
    SELECT @sqlString=Isnull(@sqlString+' UNION '+'SELECT '''+Column_Name+''' AS column_name, cast('+Column_Name+' AS nvarchar) distinct_value, COUNT(*) AS total FROM '+TABLE_NAME+' GROUP BY '+Column_Name,
                        'SELECT '''+Column_Name+''' AS column_name, cast('+Column_Name+' AS nvarchar) distinct_value, COUNT(*) AS total FROM '+TABLE_NAME+' GROUP BY '+Column_Name)
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = @TableName

    /*Executing generated dynamic sql to get the required resulsts*/
    EXEC(@sqlString)

END

EXEC getColumnsDistinctValueCount 'Movies'
-------------------------------------------------------------------------

Cheers,
Pooja Falor
Pune, India
 
Old February 22nd, 2005, 07:18 PM
Registered User
 
Join Date: Feb 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That works good. Exception to this noted below:

Server: Msg 306, Level 16, State 2, Line 1
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.

Nevertheless, I found a good inexpensive data profiling tool that goes above and beyond doing this manually. Saved me a ton of time and was extremely insightful. Check out:

http://www.datiris.com/

Thanks for your help.

Paul






Similar Threads
Thread Thread Starter Forum Replies Last Post
problem sending values into SQL Server Proc lisabb ASP.NET 2.0 Basics 2 May 23rd, 2007 01:19 PM
Text/large varchar fields in SQL server pseudomorph Classic ASP Databases 0 August 16th, 2006 11:50 PM
working with money values in sql server mattastic SQL Server 2000 3 September 29th, 2005 08:15 PM
Checking null values in Query(SQL Server) Prob leo_vinay Classic ASP Databases 2 December 28th, 2004 04:31 AM
SQL profiling Ned SQL Server 2000 1 August 20th, 2003 01:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.