一表中有若干字段,比如:
门卫 主任 干事 电工 水暖工 王二  李四 李三 刘大  老鲁王二  李四 李四 刘大  老鲁
王二  李四 李三 刘二  老鲁想得到某个职位任职次数最多的人?
如干事李三有两次任职就得到李三 电工刘大有两次就得到刘大。

解决方案 »

  1.   

    一定要写sql语句吗
    sql实现好像没什么好方法可以自己写个程序取分别对每个字段取count(*),然后按这个字段group by ,然后对比每个count的值,取出最大的
      

  2.   

    参考答案
    select a.* from (
    select '门卫' 职位, 门卫, count(1) 次数 from tablename group by 门卫 union all
    select '主任', 主任, count(1) from tablename group by 主任 union all
    select '干事', 干事, count(1) from tablename group by 干事 union all
    select '电工', 电工, count(1) from tablename group by 电工 union all
    select '水暖工', 水暖工, count(1) from tablename group by 水暖工) a,(select 职位, max(次数)次数 from (
    select '门卫' 职位, 门卫, count(1) 次数 from tablename group by 门卫 union all
    select '主任', 主任, count(1) from tablename group by 主任 union all
    select '干事', 干事, count(1) from tablename group by 干事 union all
    select '电工', 电工, count(1) from tablename group by 电工 union all
    select '水暖工', 水暖工, count(1) from tablename group by 水暖工)a group by 职位 
    ) b where a.职位 = b.职位 and a.次数=b.次数
      

  3.   

    create Table #tmp(
             门卫 varchar(20),
             主任 varchar(20),
             干事 varchar(20),
             电工 varchar(20),
             水暖工 varchar(20)
         )
    goinsert into #tmp(门卫,主任,干事,电工,水暖工) 
    select '王二',  '李四', '李三', '刘大',  '老鲁'
    union all 
    select '王二',  '李四', '李四', '刘大',  '老鲁' 
    union all
    select '王二',  '李四', '李三', '刘二' , '老鲁' 
    select dept,门卫,count(*)num from 
    (
    select '门卫' dept,门卫 from #tmp
    union all
    select '主任'dept,主任 from #tmp
    union all
    select '干事'dept,干事 from #tmp
    union all 
    select '电工'dept,电工 from #tmp 
    union all
    select '水暖工'dept,水暖工 from #tmp)   addd
    group by dept,门卫 
      

  4.   

    create Table #tmp(
             门卫 varchar(20),
             主任 varchar(20),
             干事 varchar(20),
             电工 varchar(20),
             水暖工 varchar(20)
         )
    go
    delete from #tmp
    insert into #tmp(门卫,主任,干事,电工,水暖工) 
    select '王二',  '李四', '李三', '刘二',  '老鲁'
    union all 
    select '王二',  '李四', '李四', '刘大',  '老鲁' 
    union all
    select '王二',  '李四', '李三', '刘大' , '老鲁' --按如下结构即可, 和starluck一样,只是满足了一下"想得到某个职位任职次数最多的人"的需求
    select top 1 '电工' as uType, 电工 as uName,count(1) as num from #tmp group by 电工 order by num desc
    --同样,starluck的语句可以稍改一下:
    select top 5 dept,uName,count(1)num from 
    (
    select '门卫' dept,门卫 as uName from #tmp
    union all
    select '主任'dept,主任 from #tmp
    union all
    select '干事'dept,干事 from #tmp
    union all 
    select '电工'dept,电工 from #tmp 
    union all
    select '水暖工'dept,水暖工 from #tmp) a 
    group by dept,uName
    order by num desc 
      

  5.   

    dept   uName                num
    ------ -------------------- -----------
    主任     李四                   3
    水暖工    老鲁                   3
    门卫     王二                   3
    干事     李三                   2
    电工     刘大                   2(5 行受影响)
      

  6.   

    楼主,表结构若象你这样设计的话,,每一个职务都需要查询一次才能得到结果,即使强行把语句揉到一块,也只是增加子查询的个数而已,职务越多,子查询越庞大。如果把职务和员工姓名抽象出来设计成一个表,如下:
    职务     姓名
    门卫     王二 
    门卫     王二
    门卫     王二
    主任     李四
    主任     李四
    主任     李四
    干事     李三
    干事     李四
    干事     李三
    电工     刘大
    电工     刘大
    电工     刘二
    水暖工   老鲁
    水暖工   老鲁
    水暖工   老鲁那么无论有多少职务,都可以用下面的语句得到结果(也许有更简单的写法,午睡要紧,没有细想)select 职务,姓名,count(*) 任职次数
    from table1 a
    group by 职务,姓名 having count(*)=(select top 1 count(*) from table1 where 职务=a.职务 group by 职务,姓名 order by count(*) desc)
    此种写法考虑了次数并列第一的情况