Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 ORDER BY items must appear in the select list?

Author  Topic 

willbourne
Starting Member

7 Posts

Posted - 2005-05-25 : 04:23:31
I get this message [ORDER BY items must appear in the select list if SELECT DISTINCT is specified.] when I added DISTINCT to my code. My CASE WHEN includes items that appears in the select list? What do I do?

SELECT DISTINCT
c.customerId,
c.socialSecurityNumber,
c.customersName,
c.phoneNumber,
c.emailAddress,
c.languageName,
c.targetGroupName,
c.customerResponsibleId,
c.customerResponsibleName
FROM Customers AS c
LEFT JOIN
UserSettings AS u
on u.AllowUserToSee = @userid

WHERE (c.customerResponsibleId = @userid or c.customerResponsibleId = u.UserId)
ORDER BY
CASE
WHEN @sortorder = 'name' then customersName
WHEN @sortorder = 'latestContact' then customersName
WHEN @sortorder = 'birthDate' then socialSecurityNumber
WHEN @sortorder = 'totalHolding' then customersName
END

domus71
Starting Member

2 Posts

Posted - 2005-05-25 : 04:41:13
When you use GROUP BY or DISTINCT, then ORDER BY items must appear in the select list !
Go to Top of Page

shijobaby
Starting Member

44 Posts

Posted - 2009-08-19 : 05:38:34
Hi

The reasons and ways to avoid this error have discussed in this

site with good examples. By making small changes in the query

http://sqlerror104.blogspot.com/2009/08/order-by-items-must-appear-in-select_19.html
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-08-19 : 10:47:49
wow! pulling up 4 year old topics!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-08-21 : 06:29:32
quote:
Originally posted by russell

wow! pulling up 4 year old topics!


See all his posts. He is targetting only old threads

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -