• webrunner (7/26/2010)


    Good question, interesting to learn that the parentheses make no difference for SELECT DISTINCT.

    I have one question, though. I've seen people show me queries where they write SELECT COUNT(DISTINCT userid) or something like that. Is that also the same as SELECT DISTINCT COUNT(userid), or is there a difference?

    And I am also curious about how (1) SELECT COUNT(DISTINCT userid) would differ from (2) SELECT COUNT(userid) - I assume if there are multiple rows with the same userid in the table being queried, the second query would return more rows than the first? **

    ** Edited again - sorry, I guess these queries would return the same number of rows (e.g. one summary row), but maybe different COUNT values. Is that correct?

    Thanks,

    webrunner

    AS per The Count() BOL Entry

    The Distinct Keyword is a argument of the Count() Function and is not the same as when used like SELECT DISTINCT col1, col2 from mytable;

    For that use reference the SELECT Clause BOL Entry that states:

    DISTINCT

    Specifies that only unique rows can appear in the result set. Null values are considered equal for the purposes of the DISTINCT keyword.

    -Luke.

    Edited to fix quote.

    To help us help you read this[/url]For better help with performance problems please read this[/url]