try:
select top n from [table] group by classid

解决方案 »

  1.   

    try:
    select top n * from [table] group by classid
      

  2.   

    N为数字:select * from 表 t
    where Name in (select top N Name 
                      from 表
                         where ClassID=t.ClassID 
                             order by Name)
      

  3.   

    select 
        a.* 
    from 
        [table] a 
    where 
        a.Name in(select top n Name from [table] where ClassID=a.ClassID)
      

  4.   

    你的classid相同的name可能相同吗?如果不相同楼上的就正确
    否则就不对
      

  5.   

    如果name有可能相同
    就加一个自动增长的列id标识一下
    改成:
    select 
        a.* 
    from 
        [table] a 
    where 
        a.id in(select top n id from [table] where ClassID=a.ClassID)
      

  6.   

    我说清楚点.
    表 Student:
    -----------------------------
    ClassID     |    StudentName   
    ------------|----------------
    1                   a
    1                   b
    1                   c
    2                   d
    2                   e
    2                   f
    3                   g
    3                   h
    --------------------------------需要的结果集:(各班取2个)
    -----------------------------
    ClassID     |    StudentName   
    ------------|----------------
    1                   a
    1                   b
    2                   d
    2                   e
    3                   g
    3                   h
      

  7.   

    select * from 表 a where (select count(1) from 表 b where a.ClassID=b.ClassID 
    and a.StudentName<=b.StudentName)<=2
      

  8.   

    --为什么不对??
    --测试环境
    declare @t table(ClassID int,StudentName varchar(20))
    insert into @t select 1,'a'
    union all select 1,'b'
    union all select 1,'c'
    union all select 2,'d'
    union all select 2,'e'
    union all select 2,'f'
    union all select 3,'g'
    union all select 3,'h'--查询
    select * from @t A
    where StudentName in 
    (select top 2 StudentName from @t where ClassID=A.ClassID
     Order by ClassID)
    --结果
    ClassID     StudentName          
    ----------- -------------------- 
    1           a
    1           b
    2           d
    2           e
    3           g
    3           h(所影响的行数为 6 行)
      

  9.   

    declare @t table
    (ClassID int,StudentName varchar(10))   
    insert into @t values (1,'a')
    insert into @t values (1,'b')
    insert into @t values (1,'c')
    insert into @t values (2,'d')
    insert into @t values (2,'e')
    insert into @t values (2,'f')
    insert into @t values (3,'g')
    insert into @t values (3,'h')select * from @t a where (select count(1) from @t b where a.ClassID=b.ClassID 
    and a.StudentName<=b.StudentName)<=2ClassID     StudentName 
    ----------- ----------- 
    1           b
    1           c
    2           e
    2           f
    3           g
    3           h(所影响的行数为 6 行)