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

SQL 查询问题 在线等
A                 B
1   2006/11/30        
0 2006/12/31
1 2006/9/30
1 2006/11/30
0 2006/6/30
0 2006/3/30
1 2006/4/30

把A列值=1   并且   B列按时间排序   最新的排在最前面
谢谢

------解决方案--------------------
create table T(A int, B datetime)
insert T select 1, '2006-11-30 '
union all select 0, '2006-12-31 '
union all select 1, '2006-9-30 '
union all select 1, '2006-11-30 '
union all select 0, '2006-6-30 '
union all select 0, '2006-3-30 '
union all select 1, '2006-4-30 '

select * from T
order by (case when A=1 then -1 else A end), B desc

A B
----------- ------------------------------------------------------
1 2006-11-30 00:00:00.000
1 2006-11-30 00:00:00.000
1 2006-09-30 00:00:00.000
1 2006-04-30 00:00:00.000
0 2006-12-31 00:00:00.000
0 2006-06-30 00:00:00.000
0 2006-03-30 00:00:00.000

(7 row(s) affected)