Wednesday, February 25, 2009

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

Today another error is appearing where the ORDER BY has a field in it where the SELECT DISTINCT list doesn't.  And SQL server is throwing an error.  How nice.

Here is a code example that will cause an error:

select distinct
     datepart(dd,visitDate), fld2
from tbl
order by visitDate



Fix  :

select distinct

     datepart(dd,visitDate), fld2

from tbl

order by datepart(dd,visitDate)




0 comments:

Post a Comment