Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORDER BY items must appear in the select list if SELECT DISTINCT is specified

I added the columns in the select list to the order by list, but it is still giving me the error:

ORDER BY items must appear in the select list if SELECT DISTINCT is specified.

Here is the stored proc:

CREATE PROCEDURE [dbo].[GetRadioServiceCodesINGroup]  @RadioServiceGroup nvarchar(1000) = NULL AS BEGIN SET NOCOUNT ON;  SELECT DISTINCT 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   ORDER BY rsc.RadioServiceCode,rsc.RadioServiceCodeId,rsc.RadioService  END 
like image 915
Xaisoft Avatar asked Nov 05 '08 16:11

Xaisoft


People also ask

Can distinct be used with ORDER BY?

Either DISTINCT doesn't work (because the added extended sort key column changes its semantics), or ORDER BY doesn't work (because after DISTINCT we can no longer access the extended sort key column).

Does ORDER BY have to be in select?

Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement. There's another exception, when you're using SELECT DISTINCT you must include the fields used in the GROUP BY clause in the select list.

Does ORDER BY column need to be in select?

Notes. If SELECT DISTINCT is specified or if the SELECT statement contains a GROUP BY clause, the ORDER BY columns must be in the SELECT list. An ORDER BY clause prevents a SELECT statement from being an updatable cursor.

What is select distinct in SQL?

The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.


1 Answers

Try this:

ORDER BY 1, 2 

OR

ORDER BY rsc.RadioServiceCodeId, rsc.RadioServiceCode + ' - ' + rsc.RadioService 
like image 188
Chris Van Opstal Avatar answered Sep 20 '22 08:09

Chris Van Opstal