一个对于我这个新手感觉有点难的sql语句,求大家帮忙啊.两个表 tb1:单位,部门,人数
       tb2:单位对tb2的每个记录(即每个单位),统计出该单位在tb1中的部门人数平均值,其中若tb1中该单位部门超过3个,则取人数最少的3个部门人数取平均

解决方案 »

  1.   

    select a.单位,(
    select avg(人数) as 平均人数 
    from (
    select top 3 * from tb1 where 单位=a.单位 order by 人数
    ) as t
    ) as 平均人数
    from tb2 a
      

  2.   

    测试:declare @tb1 table(
    单位 varchar(10),
    a varchar(10),
    人数 int
    )
    insert @tb1 select 'a','a',2
    union all select  'a','b',3
    union all select  'a','c',4
    union all select  'a','d',5
    union all select  'b','a',3
    union all select  'b','b',5declare @tb2 table(
    单位 varchar(10)
    )
    insert @tb2 select 'a'
    union all select  'b'
    union all select  'c'select a.单位,(
    select avg(人数) as 平均人数 
    from (
    select top 3 * from @tb1 where 单位=a.单位 order by 人数
    ) as t
    ) as 平均人数
    from @tb2 a--结果
    单位         平均人数        
    ---------- ----------- 
    a          3
    b          4
    c          NULL(所影响的行数为 3 行)
      

  3.   


    declare @tb1 table(unit nchar(10),dept nchar(20),peo int)
    insert into @tb1 
    select '1','a',10
    union
    select '1','b',20
    union 
    select '1','c',3
    union 
    select '1','d',8declare @tb2 table(unit nchar(10))
    insert into @tb2
    select '1'
    union
    select '2'select a.unit,avg(b.peo)
    from @tb2 a
    left join (select top 3 unit,peo from @tb1 order by peo asc) b
    on a.unit = b.unit
    group by a.unit
      

  4.   

    select m.单位 , isnull(n.人数,0) as 人数 from tb2 m
    left join
    (
    select 单位 , avg(人数) as 人数 from
    (
      select * from tb1 as t
      where (select count(*) from tb1 where 单位 = t.单位 and 人数 < t.人数) < 3
    ) p
    ) n
    on m.单位 = n.单位
      

  5.   

    select m.单位 , isnull(n.人数,0) as 人数 from tb2 m
    left join
    (
    select 单位 , avg(人数) as 人数 from
    (
      select * from tb1 as t
      where (select count(*) from tb1 where 单位 = t.单位 and 人数 < t.人数) < 3
    ) p
    group by 单位
    ) n
    on m.单位 = n.单位
      

  6.   

    declare @tb1 table(unit nchar(10),dept nchar(20),peo int)
    insert into @tb1 
    select '1','a',10
    union
    select '1','b',20
    union 
    select '1','c',3
    union 
    select '1','d',8declare @tb2 table(unit nchar(10))
    insert into @tb2
    select '1'
    union
    select '2'select a.unit,avg(b.peo)
    from @tb2 a
    left join (select top 3 unit,peo from @tb1 where peo > 0 order by peo asc) b
    on a.unit = b.unit
    group by a.unit
      

  7.   

    select a.unit,avg(b.peo)
    from @tb2 a
    left join (select top 3 unit,peo from @tb1 where peo > 0 order by peo asc) b
    on a.unit = b.unit
    group by a.unit
      

  8.   

    select a.unit,avg(peo)
    from @tb2 a,
    (select top 3 * from @tb1 where peo > 0 order by peo desc ) b 
    where a.unit = b.unit group by a.unit
      

  9.   


    改一下,错了!改成如下:
    select a.unit,avg(b.peo)  ----丢了b 
    from @tb2 a,
    (select top 3 * from @tb1 where peo > 0 order by peo asc ) b  --应该为asc 否则不对
    where a.unit = b.unit group by a.unit
      

  10.   

    粼漪啊,我试了你的语句,出现了"试图执行的查询中不包括作为合计函数一部分的特定表达式'a.unit'"啊
      

  11.   

    select m.单位 , isnull(n.人数,0) as 人数 from tb2 m
    left join
    (
    select 单位 , avg(人数) as 人数 from
    (
      select * from tb1 as t
      where (select count(*) from tb1 where 单位 = t.单位 and 人数 < t.人数) < 3
    )  as p
    group by 单位
    ) as n
    on m.单位 = n.单位
      

  12.   

    可能我前面说的不是很清楚,所以有的回复的sql语句明显是有误解的.
    我举个例子吧
    tb1: 单位    部门     人数
          甲      a        2
          甲      b        4
          甲      c        0
          甲      d        18
          甲      e        6
          甲      f        
          乙      a        8
          乙      b        10
          乙      c        0
          乙      d        3
          丙      a        
          丙      b        7
    这时输出应该是:    单位   部门平均人数
                        甲         4    (2、4、6的平均,0、18、null都不参与)
                        乙         7    (8、10、3)
                        丙         7
    罗嗦了一点,不过一下子就可以让大家看清楚意思了。就是首先0,null都不参与运算,其次如果某个单位部门超过3个,则取人数较少的三个部门来计算该单位部门人数的平均值。
      

  13.   

    这么多人回答了
    http://www.quandi.cn/WebForm1.aspx?quandi_id=lxzm1001
    圈地网的价值
      

  14.   

    哦,上面忘了tb2: 单位
                      甲
                      乙
                      丙
    也许tb1中还有丁、戊...等部门,但是只输出tb2里有的。
    呵呵,不知道我是不是给大家出了个难题,但是的确是我需要解决的问题,请大家帮助哦。
    顺便谢谢前面已经帮助我的的各位热心前辈,继续哦。