日期:2014-05-19  浏览次数:20414 次

送分!!
declare   @t   table(id   int   primary   key,coll   decimal   (10,2))
insert   @t   select   1,26.21
union   all   select   2,4.21

union   all   select   3,76.55
union   all   select   4,58.02
union   all   select   10,53.02

union   all   select   11,66.35
union   all   select   8,52.10
union   all   select   9,12.25

应该很简单,可我想半天没想出来       就是怎么选出中间那部分数据

------解决方案--------------------
什么规律?


------解决方案--------------------
select *
from @t
where id in (3, 4, 10)
------解决方案--------------------
declare @t table(id int primary key,coll decimal (10,2))
insert @t select 1,26.21
union all select 2,4.21

union all select 3,76.55
union all select 4,58.02
union all select 10,53.02

union all select 11,66.35
union all select 8,52.10
union all select 9,12.25

seelct top 3 * from @t
where id not in (
select top 2 id from @t
)

------解决方案--------------------
估计不能做到吧,id自动按照升序排列的,取不到
------解决方案--------------------
id 即然是primary key,insert时已经排序了
你这个不加其它字段没办法的
------解决方案--------------------
除非id不为主键,才可以
------解决方案--------------------
SELECT TOP * FROM @t WHERE id NOT IN(SELECT TOP 2 id FROM @t)
------解决方案--------------------
declare @t table(id int primary key,coll decimal (10,2))
insert @t select 1,26.21
union all select 2,4.21

union all select 3,76.55
union all select 4,58.02
union all select 10,53.02

union all select 11,66.35
union all select 8,52.10
union all select 9,12.25

DECLARE @COUNT INT
DECLARE @A INT
DECLARE @B INT
DECLARE @C INT

SELECT @COUNT = COUNT(*) FROM @t
SELECT @COUNT
SELECT @A = @COUNT/3 --- 上

IF (@COUNT%3 = 1)
BEGIN
SELECT @C = @A +1
SELECT @B = @A
END
ELSE
IF (@COUNT%3 = 2)
BEGIN
SELECT @B = @A+1
SELECT @C = @A+1
END
ELSE
IF (@COUNT%3 = 0)
BEGIN
SELECT @B = @A
SELECT @C = @A
END

SELECT @A ---- 上
SELECT @B ---- 中
SELECT @C -----下

select top @B * from @t where id not in ( select top @A id from @t)
------解决方案--------------------
哦..不明你最后要的是什么