Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL DISTINCT on Multiple Columns


SQL DISTINCT on Multiple Columns

Author
Message
pgoldy
pgoldy
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 43
Jason. Right - I understood that. Just wanted to point out to Sarvesh that he should make his article explanation match the query and produce expected results. Maybe I'm being to picky...


P Goldy
gregg_dn
gregg_dn
Mr or Mrs. 500
Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)Mr or Mrs. 500 (527 reputation)

Group: General Forum Members
Points: 527 Visits: 1030
Enjoyed the article especially the comparison between DISTINCT and GROUP BY.
Jason-299789
Jason-299789
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1132 Visits: 3229
pgoldy (2/8/2011)
Jason. Right - I understood that. Just wanted to point out to Sarvesh that he should make his article explanation match the query and produce expected results. Maybe I'm being to picky...


Apologies, I miss read your post (end of a long day)

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36042 Visits: 18736
The input data has been fixed.

If this article was something you already knew, then it wasn't for you, and I'm not sure it makes sense for you to rate it lower. We don't want to put out all articles that examine performance or detailed dissection of the workings of a function. We do try to make sure that the people that started learning T-SQL this last month have some content as well.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Andy DBA
Andy DBA
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 753
I did not find this to be a good introductory article on DISTINCT. I gave this article 1 star and I have "the nads to say why".

I often see SELECT DISTINCT when doing code reviews. To me it's a red flag: Either a junior developer didn't fully understand why duplicates were being returned and did a band-aid fix by slapping on a "SELECT DISTINCT" or, they blindly copied code from some query generator that automatically adds DISTINCT whether it's needed or not.

Also, as another poster pointed out, in the section for finding duplicate addresses (where the mysterious "AddressTable" first appears), the two "equivalent" queries are NOT the same at all! I'm not sure what the author is trying to show here, but I'm surprised the example code was published without any testing to confirm it accurately illustrates the authors point (whatever that is). I'll bet "many a newbie" will be thoroughly confused or mislead by it, though. I just hope this mistake was caused by an editing error and not by a lack of understanding.


I'm also surprised that, although this article attempts to point out similarities between DISTINCT and GROUP BY, it doesn't really go into trade-offs between them. The most obvious to me is that with GROUP BY, it's very easy to retrieve additional information by adding aggregates (such as a count). I've found that often the first question asked after "what are the unique values?" is "how many rows are there for each of those unique values?". When using GROUP BY, all you have to do is add a count(*) to the select list. The next request is likely for an additional level of detail (eg. break-down by calendar year) which can easily be accomodated by adding another column to the select list and group by clause.


Finally, the example using aggregates seems to be somewhat contrived. In my experience, requests to sum distinct values are very rare, but I have had to re-write queries that incorrectly summed on results from a SELECT DISTINCT.

DISTINCTS in aggregates = BAD IDEA!

I have much to learn, so somebody please tell me What makes SELECT DISTINCT such a 'fine bit of "SQL Spackle"'.

Please sell me on any reason beside "clarity of code" or "easy solution" (read band-aid fix) where using SELECT DISTINCT would be preferable to using GROUP BY.



permap
permap
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 16
This was a good introductory article about DISTINCT, and using GROUP BY.

Please see posting by srivatsa.hg to correct the typo. My understanding was that the PostalCode were meant to be different for 'Sarvesh' and 'Param'.

'select * from DistinctTutorial' was meant to be 'select * from DuplicateTest'. Please verify and correct as needed.
The Grateful DBA
The Grateful DBA
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 850
I too enjoyed the article and thought it would a great time to pimp one of my own blog posts being that it's closely related to this topic.

The post covers counting based off DISTINCT clauses and the pitfalls one needs to be aware of: COUNTing with the DISTINCT Clause. I hope some of your educated audience can get something out of it to add to their knowledge base.

Thanks...Troy
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21071 Visits: 18259
Thanks for the article.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Toby White
Toby White
Mr or Mrs. 500
Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)

Group: General Forum Members
Points: 503 Visits: 639
Thanks for the article. I try not to use distinct, especially in multi joined select statements, but your article delivered on what you set out to deliver as far as I am concerned. Besides, look at the great discussion you generated!
dave hants
dave hants
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 539
Good effort Sarvesh.

See also QOTD:

http://www.sqlservercentral.com/questions/T-SQL/70386/

:-)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search