A公司有产品10个 B公司有产品10个...   10个公司有100个产品  分页时每页显示10条 搜索时 在分页中每页只能:每个公司只能有一个产品 也就是每页中不能有重复的公司

解决方案 »

  1.   

    select * 
    from (
    select *,row_number() over(partition by company order by product) as rn
    from tb
    ) t
    order by rn
      

  2.   

    如果 产品个数是不定的呢?if object_id(N'company') is not  null
    drop table company
    go
    create table  company (
    companyname varchar(2),
    product    varchar(60)
    )
    --公司1
    insert into company
    select 'A','A1' union 
    select 'A','A2' union 
    select 'A','A3' union 
    select 'A','A4' union 
    select 'A','A5' union 
    select 'A','A6' union 
    select 'A','A7' union 
    select 'A','A8' union 
    select 'A','A9' union 
    select 'A','A10'   --公司2
    insert into company
    select 'B','B1' union 
    select 'B','B2' union 
    select 'B','B3' union 

    --公司10
    insert into company
    select 'J','J1' union 
    select 'J','J2' union 
    select 'J','J3' union  
    select 'J','J8' union 
    select 'J','J9' union 
    select 'J','J10' 
      

  3.   

    select companyname,product
    from( 
      select *,rn=row_number() over(partition by companyname order by getdate()) 
      from company
    ) t
    where rn=3 --3是页码/**
    companyname product
    ----------- ------------------------------------------------------------
    A           A2
    B           B3
    J           J2
    **/
    按照楼主的提供的测试数据,第4页以后只有2条记录,第7页以后只有1条记录
      

  4.   

    参考看看http://topic.csdn.net/u/20100730/12/ACFCC31F-34F6-4A75-8CE1-D951881DB251.html