当前位置: 代码迷 >> Sql Server >> 取最大/最小的两个值解决方案
  详细解决方案

取最大/最小的两个值解决方案

热度:262   发布时间:2016-04-27 19:30:03.0
取最大/最小的两个值
表格如下:
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 #taselect '01', 2union all select '02', 4union all select '03', 7union all select '04', 0union all select '05', 1union all select '06', 3union all select '07', 9union all select '08', 11select * from( select top 2 id, numfrom #ta order by num)t1union allselect * from(select top 2 id, numfrom #ta order by num desc)t2order by num desc
  相关解决方案