select * from tablename a
where (select count(*) from tablename where sex=a.sex and name<=a.name)<=2

解决方案 »

  1.   

    select * from (select top 2 name,sex from user where sex=0 order by name union all select top 2 name,sex from user where sex=1 order by name) t
      

  2.   

    效果是像楼上的。
    但是,表:user (sex种类是n个。)
    用union分类合并它们的话,有点不太容易了。
      

  3.   

    select t.* from [user] t where t.name in(select top 2 name from [user] where sex=t.sex order by name)
      

  4.   

    楼上的好像也不行。
    我要的是像3楼的那种显示效果。只是3楼将sex种类锁定了。
      

  5.   

    create table #tmp(name varchar(200),sex int)insert into #tmp select 'aaa',1 union select 'bbb',1 union select 'ccc',0 union select 'ddd',0 union select 'eee',3 union select 'fff',1select t.* from #tmp t where t.name in(select top 2 name from #tmp where sex=t.sex order by name) order by sex,namedrop table #tmp------
    name     sex
    ccc 0
    ddd 0
    aaa 1
    bbb 1
    eee 3
      

  6.   

    select t.* from [user] t where t.name in(select top 2 name from [user] where sex=t.sex order by name)这个可以了
      

  7.   

    SELECT *
    FROM USER a
    WHERE 
    (SELECT COUNT(*) 
    FROM USER b 
    WHERE a.sex=b.sex
    AND b.name<a.name)=2
    ORDER BY sex, name