假设我有一个包含几亿行的表,看起来像这样:
memID | foo | bar | foobar 1 | blah | blah | blah 1 | blah | blah | blah 1 | blah | blah | blah 1 | blah | blah | blah 1 | blah | blah | blah 1 | blah | blah | blah 1 | blah | blah | blah 2 | blah | blah | blah 2 | blah | blah | blah 2 | blah | blah | blah 2 | blah | blah | blah 2 | blah | blah | blah . . . 10001 | blah | blah | blah 10001 | blah | blah | blah
我需要一个查询,它将返回一系列成员ID中每个memID的前N行.例如,如果N = 3且范围为0-2,则应返回
memID | foo | bar | foobar 1 | blah | blah | blah 1 | blah | blah | blah 1 | blah | blah | blah 2 | blah | blah | blah 2 | blah | blah | blah 2 | blah | blah | blah
我考虑了几种方法,首先创造了一个庞大的方法
SELECT TOP (3) * FROM table WHERE memID = 0 UNION ALL SELECT TOP (3) * FROM table WHERE memID = 1 . . .
在代码中查询.出于可能明显的原因,这不是一个真实的选择.
第二种方法是创建一个临时表,并在memID的范围内循环,将每个memID的TOP 3插入到该临时表中.
WHILE @MemID < 10000 AND @MemID > 0 BEGIN INSERT INTO tmp_Table SELECT TOP (3) * FROM table WHERE memID = @MemID SET @MemID = @MemID + 1 END
这有效,但我想知道是否有一个我更缺失的更优雅的单一查询解决方案.
Cadaeic给了我一个没有修补的答案,但是感谢所有提出分析的人,看起来我有一些认真的阅读.
declare @startID int, @endID int, @rowsEach int select @startID = 0, @endID = 2, @rowsEach = 3 select * from ( select memID, foo, bar, foobar, row_number() over (partition by dense_rank order by dense_rank) [rank_row] from ( select memID, foo, bar, foobar, dense_rank() over (order by memID) [dense_rank] from #test where memID between @startID and @endID ) a ) b where rank_row <= @rowsEach
结果:
memID foo bar foobar rank_row ----------- ---- ---- ------ -------------------- 1 blah blah blah 1 1 blah blah blah 2 1 blah blah blah 3 2 blah blah blah 1 2 blah blah blah 2 2 blah blah blah 3
如果你想在本地测试,这里是设置代码:
create table #test ( memID int not null , foo char(4) not null , bar char(4) not null , foobar char(4) not null ) insert into #test (memID, foo, bar, foobar) select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 1, 'blah', 'blah', 'blah' union all select 2, 'blah', 'blah', 'blah' union all select 2, 'blah', 'blah', 'blah' union all select 2, 'blah', 'blah', 'blah' union all select 2, 'blah', 'blah', 'blah' union all select 10001, 'blah', 'blah', 'blah' union all select 10001, 'blah', 'blah', 'blah' union all select 10001, 'blah', 'blah', 'blah'