日期:2014-05-17 浏览次数:20582 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (id int)
insert into [TB]
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 8 union all
select 7 union all
select 8 union all
select 9 union all
select 10
select * from [TB]
;WITH TT
AS(
SELECT DISTINCT ID,ROW_NUMBER() OVER(ORDER BY GETDATE()) AS NO
FROM (SELECT DISTINCT id FROM tb)a),
T1
as
(
SELECT id,ROW_NUMBER()OVER(PARTITION BY id - no ORDER BY GETDATE())AS Num FROM TT )
SELECT id FROM T1 WHERE num <=4
/*
1
2
3
4
7
8
9
10*/