Alex Rivera | Logout

SQL: count number of distinct values in every column

Asked 2009-09-01T17:04:18.723
9

I need a query that will return a table where each column is the count of distinct values in the columns of another table.

I know how to count the distinct values in one column:

select count(distinct columnA) from table1;

I suppose that I could just make this a really long select clause:

select count(distinct columnA), count(distinct columnB), ... from table1;

but that isn't very elegant and it's hardcoded. I'd prefer something more flexible.

Edit
Report

1 Answer

0

This won't necessarily be possible for every field in a table. For example, you can't do a DISTINCT against a SQL Server ntext or image field unless you cast them to other data types and lose some precision.

answered 2009-09-01T18:01:02.853

Your Answer