日期:2014-05-18  浏览次数:20550 次

求助!sql server order by 问题
select   ...   from   ...where...
ORDER   BY           SUB_TENP   DESC,   CHNG_DATE   DESC,  
CASE   WHEN   DEL_DATE   IS   NULL   THEN   DEL_DATE   ASC   ELSE   DEL_DATE   DESC   END,   TENP_KN   DESC

------解决方案--------------------
order by 只能放在最后
------解决方案--------------------
我猜你的意思是

order by ORDER BY SUB_TENP DESC, CHNG_DATE DESC,
CASE WHEN DEL_DATE IS NULL THEN '2999-01-01 ' ELSE DEL_DATE END DESC,
TENP_KN DESC

------解决方案--------------------
case when 不能用到order by 里

select ...,CASE WHEN DEL_DATE IS NULL THEN DEL_DATE ASC ELSE DEL_DATE DESC END as newfield from ...where...
ORDER BY SUB_TENP DESC, CHNG_DATE DESC,
newfield desc, TENP_KN DESC
------解决方案--------------------
试一下,原来可以放在order by 后面,只是你写错了(以前从来没这么写过)

select ... from ...where...
ORDER BY SUB_TENP DESC, CHNG_DATE DESC,
CASE WHEN DEL_DATE IS NULL THEN DEL_DATE ASC ELSE DEL_DATE DESC END, TENP_KN DESC

“DESC END” 应该反过来啊 “END DESC”
select ... from ...where...
ORDER BY SUB_TENP DESC, CHNG_DATE DESC,
CASE WHEN DEL_DATE IS NULL THEN DEL_DATE ASC ELSE DEL_DATE END DESC , TENP_KN DESC