首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

怎么取中间的三条记录

2012-03-25 
如何取中间的三条记录?有一个表,其中一个字段my_data为Float型,如何用my_data排序以后如何取正中间的三条

如何取中间的三条记录?
有一个表,其中一个字段my_data为Float型,如何用my_data排序以后如何取正中间的三条记录?谢谢!

[解决办法]

SQL code
--6条记录 或是 4条记录 测试没有问题declare @t table (my_data numeric(2,1))insert into @tselect 1.1 union allselect 1.2 union allselect 1.5 union allselect 1.6 union allselect 1.7 union allselect 1.8;with maco as(select row_number () over (order by my_data) as num,* from @t)select my_data from maco where num between (select ceiling(count(1)/2.0)-1 from maco)and  (select ceiling(count(1)/2.0)+1 from maco)/*my_data---------------------------------------1.21.51.6*/
[解决办法]
SQL code
select *from(    select *,px=row_number() over (order by my_data)    from tb)twhere px between (case when px%2=0 then px/2-1 else (px+1)/2-1 end)          and (case when px%2=0 then px/2+1 else (px+1)/2+1 end) 

热点排行