libin_ftsafe(子陌红尘) ( **) 老兄果然是两星上将, 你的方式我测试过,KO!!! 谢谢!!
其他SQL兄的方式我也试过,上面几个用sort_id不行的,谢谢!!
散分,结贴.
下面是我在db2中的测试成功的sql,数据才15405条,比较慢
select count(*) from (
SELECT a.id,a.comcode from tbl_person a
where a.id in (
select b.id from tbl_person b
where b.comcode = a.comcode order by b.comcode fetch first 5 rows only
) order by a.comcode
) as t group by t.comcode
create table book(book_id int identity(1,1),title char(20),short_id char(20))
insert into book(title,short_id)
select 'sdfsf','1'
union select 'fghfer','1'
union select 'ooor','1'
union select 'epp','1'
union select 'e2er','1'
union select '23wer','1'
union select 'egher','1'
union select 'eyuter','5'
union select 'ejker','5'
union select 'bnrgher','2'
union select 'm,rwer','2'
union select 'm,rwgher','2'
union select 'erwer','3'
union select 'kjghwer','3'
union select 'vcvgher','3'
union select 'nbghwer','3'
union select 'ryghg','3'
union select 'yuy','4'
select b.* from book b where book_id in
(select top 5 book_id from book where short_id =b.short_id)
select top 5 * from book where sort_id=1 union select top 5 * from book where sort_id=2 union select top 5 * from book where sort_id=3 union select top 5 * from book where sort_id=4 union select top 5 * from book where sort_id=5