Although not the same thing, in a sense, DISTINCT implies GROUP BY , because each DISTINCT can be rewritten using GROUP BY . With this in mind, it makes no sense to order something that is not part of the overall group.
For example, if you have a table like this:
col1 col2
---- ----
eleven
12
2 1
2 2
2 3
3 1
and then try to query it like this:
SELECT DISTINCT col1 FROM [table] WHERE col2 > 2 ORDER BY col1, col2
This does not make sense, because there may be several col2 values ββin a row. Which one should I use to order? Of course, in this query, you know that the results will not be the same, but the database server cannot know about this in advance.
Now your case is a little different. You have included all the columns from the order by clause of the select clause, and so at first glance it might seem that they are all grouped. However, some of these columns were included in the calculated field. When you do this in combination with various, distinct directives can only be applied to the final results of calculations: it knows nothing more about the source of the calculations.
This means that the server does not really know that it can count on these columns anymore. He knows that they were used, but he does not know if the calculation operation can cause an effect similar to my first simple example above.
So now you need to do something else to tell the server that the columns can be used for ordering. There are several ways to do this, but this approach should work well:
SELECT rsc.RadioServiceCodeId, rsc.RadioServiceCode + ' - ' + rsc.RadioService as RadioService FROM sbi_l_radioservicecodes rsc INNER JOIN sbi_l_radioservicecodegroups rscg ON rsc.radioservicecodeid = rscg.radioservicecodeid WHERE rscg.radioservicegroupid IN (SELECT val FROM dbo.fnParseArray(@RadioServiceGroup,',')) OR @RadioServiceGroup IS NULL GROUP BY rsc.RadioServiceCode,rsc.RadioServiceCodeId,rsc.RadioService ORDER BY rsc.RadioServiceCode,rsc.RadioServiceCodeId,rsc.RadioService