select top 1 教师名字,count(*) as 授课数
from t
group by 课程名
order by 授课数 desc

解决方案 »

  1.   

    select top 1 教师名字,count(distinct(课程名)) as 授课数
    from t
    group by 教师名字
    order by 授课数 desc
      

  2.   

    不对啊,TOP 1连教师名字都查询不出来,改成TOP 2,才有结果.
      

  3.   

    我的教师名里有空值,用 TOP1是把哪个空值给选择出来了,有没有解决办法?表1:
    课程名  授课地点 教师名字
    A         1       a
    B         2       c
    C         1       a
    d         2       b
    E         3        NULL
      

  4.   

    select top 1 教师名字,count(*) as 授课数
    from t
    group by 课程名
    order by 教师名字 desc,授课数 desc
      

  5.   

    select top 1  授课地点,count(*) as 授课数
    from t
    group by 课程名
    order by  授课地点 desc,授课数 desc
      

  6.   

    应该 可以的要不这样试试
    select distinct 教师名字
    from 表 a
    where not exists(select 1 from 表 b
                     where (select count(distinct(课程名)) from 表 where 教师名字=b.教师名字)
                          >(select count(distinct(课程名)) from 表 where 教师名字=a.教师名字)
                    )
      

  7.   

    select top 1 教师名字,count(distinct(课程名)) as 授课数
    from t
    group by 教师名字
    where 教师名字 is not null      --去空值 
    order by 授课数 desc
      

  8.   

    其实我想知道一个解决空值的方法,在VB中有空值直接用VAR函数就解决了,或者NZ()函数.
    在SQL中我每次都用 where ISNULL(字段1,0)=0 作为判断条件,这个太不方便了.
      

  9.   

    create view k
    as
    select count(*) c,dd from 表1
    group by 教师名字select max(c) from k
    其实lsxaa(小李铅笔刀) 的方法已经可以了。
      

  10.   

    where 教师名字 is not null      --去空值 我回复的最后一个不是写了么?????
      

  11.   

    这个不行么???select top 1 教师名字,count(distinct(课程名)) as 授课数
    from t
    where 教师名字 is not null      --去空值 
    group by 教师名字
    order by 授课数 desc
      

  12.   

    select top 1 教师名字,count(distinct(课程名)) as 授课数
    from t
    where 教师名字 is not null      --去空值 
    group by 教师名字
    order by 授课数 desc
    顺序 放错了,,呵呵呵    用这个吧
      

  13.   

    这个居然会对排序有依赖,如果不用DESC的话,前面我没有用DESC,它按照ASC来的,就把空值的哪个给查找出来,同时它也是最小值,空值确实去不掉,用 IS NOT NULL!
    可能是我对TOP用法不了解吧.
    我再看看帮助