Distinct count of records in a table

  • 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...

  • Whats the output of this ?

    select count(distinct *) from XXXXX

    Linto

  • select count(distinct *) from XXXXX

    The above gives me: "Incorrect syntax near '*'."

  • 
    
    SELECT COUNT(*)
    FROM
    (SELECT DISTINCT *
    FROM tablename) x

    Ever heard of a primary key?

    --Jonathan



    --Jonathan

  • ..>>..

    you can do

    select count(distinct col1), count(distinct col2),... from TableName

    But your still going to need to list each field

    ..<<..

  • Thanks Jonathan! That works great.

  • If you'd like a lightning fast solution, try this:

    SELECT count(distinct( BINARY_CHECKSUM(*))) FROM TableName

  • 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?

  • quote:


    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?


    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