Order BY items must appear in the select list if SELECT DISTINCT is specified
To resolve the ORDER BY
distinct issue, ensure the ORDER BY fields coincide with the SELECT DISTINCT fields. This aligns the desired sorting order with the distinct results.
If your SQL is throwing a tantrum, try ordering by column positions such as ORDER BY 1, 2
.
Dealing with Ambiguous Columns
Did your SQL throw a fit? If column names lead to confusion or errors, chill it out with column positions.
Handling Calculated Fields
Does your DISTINCT
have a frenemy relationship with calculated fields? Kick out DISTINCT
and invite GROUP BY
for a more harmonious party.
Concatenate to Sort
When sorting gets tough, remember, you can do the column fusion dance (aka concatenation).
Working with Stored Procedures
If you're trapped inside a stored procedure, watch out for additional sorting logic that might ghost your ORDER BY
clause.
Sorting out Troubles
If a wild error appears, time to play detective. Test different column/alias combinations and verify tables/aliases.
When to Stick with Position Numbers in ORDER BY
Are your column names causing trouble? Send them into hiding and let the column positions take the stage.
Was this article helpful?