一个对于我这个新手感觉有点难的sql语句,求大家帮忙啊.两个表 tb1:单位,部门,人数
tb2:单位对tb2的每个记录(即每个单位),统计出该单位在tb1中的部门人数平均值,其中若tb1中该单位部门超过3个,则取人数最少的3个部门人数取平均
tb2:单位对tb2的每个记录(即每个单位),统计出该单位在tb1中的部门人数平均值,其中若tb1中该单位部门超过3个,则取人数最少的3个部门人数取平均
解决方案 »
- 求接口联表语句
- SQL2000图片如何存储及读取
- |zyciis| 求两条SQL查询语句
- 高手帮帮忙,这个简单的存储过程怎么总是返回0?太郁闷了~~~
- 关于数据库的日志,高急了......
- sql 中的varchar控制在8000个字符,请问还有大的吗?怎样用?谢谢!!
- 请各位指点一下,一张表最合宜的字段数是多少??
- 从字符串转换为 datetime 时发生语法错误...
- SQL Server 2000 用top嵌套 取指定范围的纪录的问题
- 请问如何在VFP中调用API函数 例如GetVolumnInformation()我想知道A:的序列号
- 这样存数据为什么只能存入两条数据???各位大哥都进来看看错在哪里了!分不多还可以加,交流第一!
- 求问题的解决办法?搜索相似记录
select avg(人数) as 平均人数
from (
select top 3 * from tb1 where 单位=a.单位 order by 人数
) as t
) as 平均人数
from tb2 a
单位 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 行)
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
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.单位
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.单位
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
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
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
改一下,错了!改成如下:
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
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.单位
我举个例子吧
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个,则取人数较少的三个部门来计算该单位部门人数的平均值。
http://www.quandi.cn/WebForm1.aspx?quandi_id=lxzm1001
圈地网的价值
甲
乙
丙
也许tb1中还有丁、戊...等部门,但是只输出tb2里有的。
呵呵,不知道我是不是给大家出了个难题,但是的确是我需要解决的问题,请大家帮助哦。
顺便谢谢前面已经帮助我的的各位热心前辈,继续哦。