I have query where I use distinct and order by. Two columns used in order by clause are used in Select statement but they are concatenated. Once I run the query error message appeared:
Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Here is example of my query:
SELECT DISTINCT
si_last +' '+si_first AS UserName
FROM Users
ORDER BY si_last,si_first
One solution that I found is to have si_last
and si_first
selected one more time but separate. Something like this:
SELECT DISTINCT
si_last +' '+si_first AS UserName,
si_last,
si_first
FROM Users
ORDER BY si_last,si_first
Solution above seems very inefficient and I'm wondering if there is other way to work around this problem. Please if you have any suggestions please let me know. Thank you.