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

解决方案 »

  1.   

    --try
    select a.* from 表 a
    where sort_id in ( select top 5 sort_id from 表 where sort_id=a.sort_id )
      

  2.   

    select * from book t where id in(select top 5 id from book where book.id=t.id)
      

  3.   

    谢谢各位光临,  我这里的sort_id 字段的值不定死的,可能有1,2,3,5,   也可能有1 2 3 4  5  6  7 8
      

  4.   

    如果相同sort_id对应的记录中title内容不重复:select 
        a.* 
    from 
        book a 
    where 
        a.title in(select top 5 title from book where sort_id=a.sort_id)
      

  5.   

    select * from book a
    where exists ( select top 5 sort_id from book where sort_id=a.sort_id )
      

  6.   

    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)drop table book
      

  7.   

    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