Count Distinct Rows in SQL Server

Question: How do you count distinct rows in a query?

1st Try: I *thought* this would work:

select distinct count(taskid) from #temp

however, it returned the count of all taskid's.

2nd Try: then I tried keeping duplicates out to begin with, so I could just do a normal count(taskid). That wasn't fool proof.

Solution: Just out of the “what-would-happen-if-I-did-this” department:

select count(distinct taskid) from #temp

Looks totaly wrong, but it worked!??! Althought I have never heard or seen this done, I am sure people will show me up by telling me how it is “all over Books Online” and such. Whatever, for now, I “invented” it.


2 responses to “Count Distinct Rows in SQL Server”

  1. Wow, a real live INVENTOR!!!! I am so impressed

    with the immensity of your achievement. Wish I

    could be you! Dude, this is SQL 101. Go back to

    Go. Do NOT Collect $200. Do NOT get a Get Out

    of Jail Pass Free. Pay RENT on Boardwalk of $500.

    Park Place is too expensive for you.

  2. Dude, I was joking, hence the quotes. Definately don’t need your sarcasm, that’s for sure.

Leave a Reply

Your email address will not be published. Required fields are marked *