当前位置: 代码迷 >> Sql Server >> 求语句,SQL排序有关问题
  详细解决方案

求语句,SQL排序有关问题

热度:71   发布时间:2016-04-24 20:33:58.0
求语句,SQL排序问题
给table A里的数据排序,排序方式为:当字段B = b1时,按照 字段C asc,字段D asc排序,当字段B= b2时,按照 字段C desc,字段D desc排序,SQL语句应该怎么写?求助~~~
sql

------解决方案--------------------
上面B的排序不对,修正:

select *
from A
order by B ,case when B = 0 then C else 1 end,case when B = 0 then D else 1 end,
case when B = 1 then C else 0 end desc,case when B = 1 then D else 0 end desc

drop table A

/*
id  B C D
-------------------------
1 0 1 2
2 0 1 3
7 0 1 4
6 0 2 1
5 1 4 4
3 1 3 6
4 1 3 5

*/
  相关解决方案