Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««1234»»

SQL DISTINCT on Multiple Columns Expand / Collapse
Author
Message
Posted Tuesday, February 8, 2011 9:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, May 8, 2014 12:40 PM
Points: 3, Visits: 37
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
Post #1060357
Posted Tuesday, February 8, 2011 9:33 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 6:19 AM
Points: 215, Visits: 474
Enjoyed the article especially the comparison between DISTINCT and GROUP BY.
Post #1060365
Posted Tuesday, February 8, 2011 9:35 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 9:31 AM
Points: 876, Visits: 2,392
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
Post #1060369
Posted Tuesday, February 8, 2011 9:42 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 12:38 AM
Points: 33,267, Visits: 15,436
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
Post #1060381
Posted Tuesday, February 8, 2011 11:07 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, September 12, 2014 8:39 AM
Points: 147, Visits: 539
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.



Post #1060455
Posted Tuesday, February 8, 2011 11:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, March 26, 2011 5:33 PM
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.
Post #1060459
Posted Tuesday, February 8, 2011 11:41 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 8:07 AM
Points: 13, Visits: 585
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
Post #1060507
Posted Tuesday, February 8, 2011 2:03 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:27 PM
Points: 21,750, Visits: 15,447
Thanks for the article.



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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1060635
Posted Tuesday, February 8, 2011 3:28 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Friday, November 9, 2012 2:48 PM
Points: 493, Visits: 636
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!
Post #1060706
Posted Tuesday, February 8, 2011 3:51 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 3:52 AM
Points: 82, Visits: 412
Good effort Sarvesh.

See also QOTD:

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

Post #1060718
« Prev Topic | Next Topic »

Add to briefcase «««1234»»

Permissions Expand / Collapse