Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase «««123

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


Group: General Forum Members
Last Login: Tuesday, January 26, 2016 9:10 AM
Points: 2,602, Visits: 1,694
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


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



Group: General Forum Members
Last Login: Today @ 2:09 AM
Points: 15,501, Visits: 13,163
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 SQLKover.

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


Group: General Forum Members
Last Login: Monday, August 17, 2015 9:40 AM
Points: 122, Visits: 123
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



Group: General Forum Members
Last Login: Thursday, November 24, 2016 12:51 AM
Points: 2,949, Visits: 1,090
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

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



Group: General Forum Members
Last Login: Friday, December 2, 2016 3:52 AM
Points: 1,715, Visits: 1,029
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:
FROM MyTable

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

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, May 23, 2016 5:31 AM
Points: 3,615, Visits: 2,774
Nice Ez Pz question.
Post #1454834
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse