declare @t table(id int primary key,coll decimal (10,2))
insert @t select 1,26.21
union all select 2,4.21union all select 3,76.55
union all select 4,58.02
union all select 10,53.02union all select 11,66.35
union all select 8,52.10
union all select 9,12.25应该很简单,可我想半天没想出来 就是怎么选出中间那部分数据
insert @t select 1,26.21
union all select 2,4.21union all select 3,76.55
union all select 4,58.02
union all select 10,53.02union all select 11,66.35
union all select 8,52.10
union all select 9,12.25应该很简单,可我想半天没想出来 就是怎么选出中间那部分数据
from @t
where id in (3, 4, 10)
insert @t select 1,26.21
union all select 2,4.21union all select 3,76.55
union all select 4,58.02
union all select 10,53.02union all select 11,66.35
union all select 8,52.10
union all select 9,12.25seelct top 3 * from @t
where id not in (
select top 2 id from @t
)
你这个不加其它字段没办法的
3 76.55
4 58.02
8 52.10
insert @t select 1,26.21
union all select 2,4.21union all select 3,76.55
union all select 4,58.02
union all select 10,53.02union all select 11,66.35
union all select 8,52.10
union all select 9,12.25DECLARE @COUNT INT
DECLARE @A INT
DECLARE @B INT
DECLARE @C INTSELECT @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
ENDSELECT @A ---- 上
SELECT @B ---- 中
SELECT @C -----下 select top @B * from @t where id not in ( select top @A id from @t)
8 2, 3, 3
10 3, 3, 4
100 33, 33, 34SELECT @A ----第一部分
SELECT @B ---- 第中部分
SELECT @C -----第三部分