Name Code days
-----------------
aaaa A 2
bbbb B -2
cccc Q -1
dddd C 1
eeee D -3
ffff A 3
我想在SQL Sever中手动对此表进行排序。当代码是A或Q时,它应始终位于顶部。否则它应按天数排序。
它的外观应该是
Name Code days
-----------------
aaaa A 2
ffff A 3
cccc Q -1
eeee D -3
bbbb B -2
dddd C 1
答案 0 :(得分:1)
您可以使用case
进行条件排序:
select *
from your_table
order by
case when code in ('A','Q') then 0 else 1 end, -- Keep A, Q at top
case when code is ('A','Q') then code end, -- order A, Q alphabetically
days; -- order by days
答案 1 :(得分:0)
select name , code , days
from t
order by
case when code in ('A','Q')
then 0 else 10 + days
end asc;
答案 2 :(得分:-1)
表表示无序集。你不能(出于实际目的)对它们进行排序。
但是,您可以从查询中对结果集进行排序。查询表时,您可以这样做:
select t.*
from t
order by (case when code in ('A', 'Q') then 1 else 2 end), days;