October 2, 2013 at 8:10 am
easy one..
Thanks Smith..
October 2, 2013 at 8:15 am
I struck out my previous comments. I don't know what happened last night but I can't seem to duplicate the problem today. I hate it when this happens.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 2, 2013 at 11:15 am
Jeff Moden (10/2/2013)
I struck out my previous comments. I don't know what happened last night but I can't seem to duplicate the problem today. I hate it when this happens.
I think we have all done that somewhere along the way
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
October 2, 2013 at 1:51 pm
Great question. Almost overlooked the distinct in the count().
October 2, 2013 at 7:53 pm
Nice and easy - thanks, Samith!
October 3, 2013 at 12:03 am
didn't need to think twice,,,
easy one.
October 3, 2013 at 2:52 am
Hany Helmy (10/2/2013)
Easy.
Ditto.
October 3, 2013 at 3:05 am
Nice and simple question. Lot to think.. thanks Samith..
October 3, 2013 at 3:18 am
tom.w.brannon (10/2/2013)
I believe the problem is a difference in how distinct gets used in different contexts. For the following queriesselect count(distinct id) from #temp_test;
select distinct id from #temp_test;
select count(*) from (select distinct id from #temp_test) a;
the results are 3, 4 rows returned, and 4. So NULL is a distinct value but not counted as a distinct value. Maybe somebody else can answer why this makes sense.
Good work Tom.. If you use 'select count(id)' in the last select query instead of 'select count(*)' you will get the result as 3. If we don't use the field name in count(distinct) clause, it will not eliminate NULL.
October 3, 2013 at 6:28 am
Thanks!
October 4, 2013 at 1:50 am
Jamsheer (10/3/2013)
tom.w.brannon (10/2/2013)
I believe the problem is a difference in how distinct gets used in different contexts. For the following queriesselect count(distinct id) from #temp_test;
select distinct id from #temp_test;
select count(*) from (select distinct id from #temp_test) a;
the results are 3, 4 rows returned, and 4. So NULL is a distinct value but not counted as a distinct value. Maybe somebody else can answer why this makes sense.
Good work Tom.. If you use 'select count(id)' in the last select query instead of 'select count(*)' you will get the result as 3. If we don't use the field name in count(distinct) clause, it will not eliminate NULL.
Tom, a value of NULL indicates that the value does not exists and is unknown. For the DISTINCT keyword, null values are considered to be duplicates of each other. When DISTINCT is included in a SELECT statement, only one NULL is returned in the results, regardless of how many null values are encountered.Check the following link:
http://technet.microsoft.com/en-us/library/ms187831(v=sql.105).aspx
In case of count(DISTINCT), it returns the number of unique non null values. See the following link for count(DISTINCT):
October 4, 2013 at 1:56 am
In between, simple and good QOTD.
October 7, 2013 at 12:11 am
easy
October 17, 2013 at 2:32 am
easy one
Distinct uses null values normally
count() eliminates them as well as all the other arrgegates functions
Viewing 14 posts - 16 through 29 (of 29 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy