当前位置: 代码迷 >> Sql Server >> !sql server order by 有关问题
  详细解决方案

!sql server order by 有关问题

热度:42   发布时间:2016-04-27 16:55:44.0
求助!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
  相关解决方案