日期:2014-05-17 浏览次数:20457 次
select * from tab order by case when ShuLiang>0 then 0 else 1 end,ID
drop table tab
go
create table tab(ID int,Name varchar(10), ShuLiang int)
insert into tab
select 1 ,'a', 5
union all select 2 ,'b', 1
union all select 3 ,'a', -1
union all select 4 ,'c', 5
select *
from tab
order by case when ShuLiang > 0
then 0
else 1
end,
id
/*
ID Name ShuLiang
1 a 5
2 b 1
4 c 5
3 a -1
*/
CREATE TABLE tab(ID int,name varchar(10),ShuLiang int)
GO
INSERT INTO tab
SELECT 1,'a',5 UNION ALL
SELECT 2,'b',1 UNION all
SELECT 3,'a',-1 UNION all
SELECT 4,'c',5
GO
SELECT * FROM tab
ORDER BY (CASE WHEN ShuLiang > 0 THEN 1 ELSE -1 END) DESC,ID