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

取最大/最小的两个值
表格如下:
id num 
01 2
02 4
03 7
04 0
05 1
06 3
07 9
08 11

希望得到如下结果

id num
08 11
07 9
05 1
04 0



------解决方案--------------------
SQL code
create table #ta
(id varchar(5), num int)

insert #ta
select '01', 2
union all select '02', 4
union all select '03', 7
union all select '04', 0
union all select '05', 1
union all select '06', 3
union all select '07', 9
union all select '08', 11

select * from
( 
select top 2 id, num
from #ta order by num
)t1
union all
select * from
(select top 2 id, num
from #ta order by num desc)t2
order by num desc