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 «««123

Distinct Expand / Collapse
Author
Message
Posted Wednesday, May 8, 2013 10:36 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 1:18 PM
Points: 2,266, Visits: 1,320
Revenant (5/8/2013)
manik123 (5/8/2013)
Lokesh Vij (5/6/2013)
Thanks for the easy one Vinay, after tricky Qotd yesterday


+1 nice question....

+ another 1, with thanks


Same!


Not all gray hairs are Dinosaurs!
Post #1450692
Posted Friday, May 10, 2013 5:27 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:49 PM
Points: 13,283, Visits: 10,156
Thanks for the question.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1451527
Posted Friday, May 10, 2013 8:49 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Tuesday, July 1, 2014 12:57 AM
Points: 112, Visits: 88
I don't understand why so many people are so happy to be presented with a simple question, or so pleased with themselves for getting it right. personally I prefer something which makes me think.

Having said that, it depends on your collation. If you're using case sensitive collation, then statements 2,3,4 and 5 would all fail.
Post #1451630
Posted Monday, May 13, 2013 11:03 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, August 5, 2013 3:22 PM
Points: 7, Visits: 11
Hey Guys

i tested the query out for this and it ran fine when i changed CAPS to the field names
Post #1452232
Posted Wednesday, May 15, 2013 2:01 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, July 10, 2014 3:25 PM
Points: 2,133, Visits: 867
Good Question.
A few people getting carried away with Collation Sequences where it looks to me that the QOTD Author was concentrating on the last Select Statement.
I like to think of the "Order By" as the Query's Presentation Layer. In this case the "Order By" couldn't present Data not in the Query's Results

Regards
David
Post #1452963
Posted Thursday, May 16, 2013 8:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, July 4, 2014 9:03 AM
Points: 1,415, Visits: 796
Easy today - what Hugo says is axiomatic.

Hugo Kornelis (5/7/2013)
Thanks! Good question.

In addition to the explanation given: the restriction actually makes sense. Remember that, logically, the ORDER BY is performed *after* the SELECT. In the official ANSI standard, columns in the ORDER BY must always be in the SELECT clause. The fact that we usually can order by other columns is because, logically, SQL Server adds a hidden extra column in the SELECT, uses it for ordering, then doesn't disply it or return it to the client. (And I stress that this is "logically" - queries are not actually executed that way!). But with a SELECT DISTINCT, that is impossible. Adding the extra column before the DISTINCT would influence the results (the extra columns could make rows that are otherwise the same distinct to each other, and once removed the results would show duplicate rows in spite of the DISTINCT). And adding the extra column after the DISTINCT is impossible, since a single row after DISTINCT can correspond to multiple rows before DISTINCT.

Or, yet another way to put it, suppose I have this data in a table called MyTable:
Col1 | Col2
-----+-----
1 | a
2 | b
3 | a

Now suppose I were allowed to execute this query:
SELECT DISTINCT Col2
FROM MyTable
ORDER BY Col1;

The resultset should contain an "a" and a "b". But in what order? The "b" comes from the row with Col1 = 2, but the "a' comes from two rows, with Col1 = 1 and Col1 = 3. Should the "a" go before or after the "b"? There is no possible answer for this - and that's why the query is illegal!


Raghavendra Mudugal (5/7/2013)
I guess, when we DISTINCT, it also sorts the data physically, so usage of additional ORDER BY is not needed

WRONG!!!!!!
This may or may not work correctly, but you have no guarantee.
The optimizer had different ways to implement a DISTINCT. One of them is a "Distinct sort" - where rows are sorted and duplicate are removed. That would produce results in the specified output - though the optimizer could decide to reverse the order of the columns. Another way would be to convert the DISTINCT to an aggregate - remember that there is no difference at all between "SELECT DISTINCT Col1 FROM MyTable" and "SELECT Col1 FROM MyTable GROUP BY MyCol", and the optimizer knows that. If the aggregeate is implemented with a stream aggregate operator. you may still be okay (though, again, the order of the columns if more than one is used can be changed to match an existing index and avoid a sort step). But if a hash aggregate operator is used, you're completely hosed.
And then, with a large enough table, you can get a parallel plan, where each individual stream might or might not have the rows in order, but the order is not retained when gathering streams. Or, with a complex query, the optimizer might decide to push down the distinct operator as far as possible to reduce the rows, and then reorder the results coming out of it for the rest, e.g. to facilitate a merge join, or as a byproduct of a hash join.

When working with SQL Server, if you need guarantees about the order in which results will be returned - ALWAYS USE AN ORDER BY!!!!!
Everything else means you rely on undocumented, and hence unguaranteed behaviour. A ticking timebomb!
Post #1453582
Posted Tuesday, May 21, 2013 1:10 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, July 21, 2014 6:24 AM
Points: 3,537, Visits: 2,647
Nice Ez Pz question.
Post #1454834
Posted Thursday, June 12, 2014 7:08 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, July 24, 2014 7:11 AM
Points: 2,382, Visits: 177
A lot of chatter on this one.
Post #1580040
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse