SQL Question - The Answers --
Posted in 1996
> If I have the following SQL
> select column1, count(*) from tablefoo
> group by column1>
> It returns 24 rows with various counts.
>
> Is there a way to return just the number of rows that would have been
> returned. (ie just the number 24)
> I know I could put it in a temp table and then count that but, yuk
>
>
I received several answers to this, the one below being the best
overall answer EXCEPT for ONE problem ( yours and my friend the NULL field)
select count( distinct column1 ) from tablefoo ? :)
I found in testing the following will occur
If I have table : foo
with columns col1 char(1)
And data of A, A, B, C, NULL, NULL, SPACE where NULL is a null value
and space is a space character.
If I type select col1, count(*) from foo group by col1
I get: A 2
B 1
C 1
2 # where this would be the nulls
1 # and this would be the space
A total of 5 rows returned, of course I could have put this into a
temp table and then counted the rows.
If I type select count( distinct col1) from foo
or I type select count( unique col1) from foo
I get the result of '4'
Though as you can see above the result is 5.
I also tried select count( distinct col1) from foo where col1 is null
and the result was 0
**MORAL**
If you can guarantee that your column(s) do not have nulls,
then the distinct will work find, otherwise you may have to do the
more round about method with the temp table.
--
Rick Tucker
rickt@wybbs.wynalda.com
-- I try to take one day at a time..., but lately several days
have attacked me at once. --