Let’s look at an example. Suppose we have the following data:
—- ——
A 1
A 1
B 3
B 3
C 2
From that, let’s say we’d like see only DISTINCT letters, ordered by value. Clearly, this means we want to return:
C
B
… right? “A” has the lowest values of 1, followed “C” with a value of 2, and finally “B” with values of 3. Yet, if we write:
from x
order by Value
we receive back the above error message. Why?
Remember, the “rules” of what is allowed and not allowed in SQL is not determined by how your data looks at a particular point in time; it is a constant. Just because one set of data seems to be perfectly orderable in this scenario does not mean that all data is always going to be perfectly orderable as well. What if we had this set of data instead:
—- —–
A 1
A 1
B 3
B 0
C 2
Look carefully at the above, and think about what should be returned when asking for distinct Letters ordered by Value.
The answer is …. there is no correct answer! B contains a value of both 0 and 3, so should it appear first, or should it appear last? The answer is — who knows?! It is not specified when simply asking for distinct Letters ordered by Value. It is not a clear, complete, deterministic question that you are asking SQL Server to answer; it is like asking someone “what color is it when you add 4 and 3?” — the question makes no sense and cannot be conclusively answered as stated.
Now, one person might say, “well, clearly, we want to B to be returned first, since it has the value of 0 and that is lower than all the other values”, and another might say “well, clearly, we want B to be returned last, since it has the value of 3 and that is clearly higher than all the other values.” And both people would technically be right, and both are entitled to their logic and their needs — but that logic and those requirements need to be specified; they cannot be implied, since there is more than one way to interpret the question. Computers are funny that way, right? They are pretty darn smart, but they always insist on being told exactly what it is we want. How annoying!
(I often use this analogy regarding users or programmers who refuse to express their requirements or needs logically and completely: It is like a magic Genie saying “you can have anything in the world, all you need to do is tell me specifically what you want and make it perfectly clear and you will get it!”, and the users thinking about it, and eventually concluding “nahh… not worth it, sounds like too much work; can’t you just figure out what I want?”)
So, what is the solution here? To make the error “just go away”, we can try just adding the Value column to the SELECT clause like this:
from x
order by Value
In our original set of data, this works fine — it returns the results we want and expect, only with an additional column. If your data has a 1-1 relation between the ORDER BY columns and the SELECT columns, then simply adding the ORDER BY columns to your SELECT does the trick.
However, running that SELECT on the second set of data (in which “B” has values of 0 and 3) results in “B” being repeated twice:
A 1
C 2
B 3
To solve this, like usual, we simply must decide what we want, and explicitly tell SQL Server what that is. If we want to order by the letter using the lowest associated value, then we simply ask for that … not by using DISTINCT, but by using GROUP BY:
from x
group by Letter
order by min(Value)
This results in:
A
C
… And if we want to do the opposite, and order by the Letter with the greatest associated value, then we write:
from x
group by Letter
order by max(Value)
A
C
B
We may even want to add up all of the values for each letter and order by that, or maybe there is some other rule. The key is that SQL Server cannot help you until you specifically express what you are looking for. And that is what this error message is all about — “Hey, what you are asking for can be interpreted many different ways; can you be more specific?”
SQL (and any other programming language) really is like the magic Genie; it can give you whatever you want, but there is a catch — you need to ask for it!
Reorder the second set of data to something like [a 1, a 3, b 0, b 3, c 2, c 1] and I think the data returned from the first SQL statement will be b, a, c. I think MySQL finds the first instance of Letter under DISTINCT and ignores other instances of the same value(letter). Since b comes first and has the value of 3, the other instance of b which has the value of 0 is ignored. I think, but i am not sure. I have to go, i’ll try this later.
I know you are being abstract since in a real database you don’t know what value will come first.
By: aptfort on May 12, 2009
at 10:24 am