有一个问题,我有ABCDEFG7个组,每组的人数不等,每个人都有一部分分数,我想取出每组的前两名,应该怎么做.
字段如下分部
组名,人的编号,分值,
应该怎么做?

解决方案 »

  1.   

    select * from tbl as a
    where 编号 in (select top 2 编号 from tbl
                   where 组名=a.组名 order by 分值 desc)
      

  2.   

    declare @table table
    (组名 int,人的编号 int,分值 int)insert into @table
    select 1,1,90
    union
    select 1,2,91
    union
    select 1,3,91
    union
    select 1,4,92
    union
    select 2,1,80
    union
    select 2,2,85
    union
    select 2,3,91select 组名,人的编号,分值
    from @table a
    where 2>(select count(*) from @table where 组名=a.组名 and 分值>a.分值)