November 12, 2003 at 8:03 am
How can I get a distinct count of all records in a table without having to list every field in the table. I also only want to return one record with the total distinct count?
I am currently using the following:
SELECT count(*)
FROM tablename
GROUP BY field1, field2, etc...
November 12, 2003 at 9:20 am
Whats the output of this ?
select count(distinct *) from XXXXX
Linto
November 12, 2003 at 9:26 am
select count(distinct *) from XXXXX
The above gives me: "Incorrect syntax near '*'."
November 12, 2003 at 9:28 am
SELECT COUNT(*)
FROM
(SELECT DISTINCT *
FROM tablename) x
Ever heard of a primary key?
--Jonathan
--Jonathan
November 12, 2003 at 9:37 am
..>>..
you can do
select count(distinct col1), count(distinct col2),... from TableName
But your still going to need to list each field
..<<..
November 12, 2003 at 9:37 am
Thanks Jonathan! That works great.
November 13, 2003 at 11:18 am
If you'd like a lightning fast solution, try this:
SELECT count(distinct( BINARY_CHECKSUM(*))) FROM TableName
November 13, 2003 at 11:36 am
SELECT count(distinct( BINARY_CHECKSUM(*))) FROM TableName
The above did run faster at 2:20 (Jonathan's method is 3:03) however, it comes up 145 records shorter. Why would that be?
November 13, 2003 at 1:12 pm
quote:
SELECT count(distinct( BINARY_CHECKSUM(*))) FROM TableNameThe above did run faster at 2:20 (Jonathan's method is 3:03) however, it comes up 145 records shorter. Why would that be?
Because rows can differ and still have the same checksum. Checksums are for hashes, not for equivalence testing; all that is guaranteed is that identical values will have the same checksum, not that the same checksum can not be created for different values.
The checksum functions return int values, so there can only be four bytes (2^32) worth of unique checksums. If your rows contain similar values, there is a high probability with large tables that some distinctly different rows will have the same checksums.
--Jonathan
--Jonathan
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply