读书人

怎么取中间的三条记录

发布时间: 2012-03-25 20:55:17 作者: rapoo

如何取中间的三条记录?
有一个表,其中一个字段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) 

读书人网 >SQL Server

热点推荐