求sql语句。

ruhong 2014-01-03 07:59:41

表a
部门序号 部门名称
1 办公室
2 人事处

表b
id 姓名 部门
1 王x 1
2 李x 1
3 刘x 2
4 张x 2

表c
序号 员工id 得分
1 1 2
2 2 3
3 3 5
4 4 2

求'办公室'的人均得分.




...全文
139 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
LongRui888 2014-01-04
  • 打赏
  • 举报
回复
引用 11 楼 ruhong 的回复:
办公室2个人,结果就是(2+3+2.5+1.3)/2=4.4
是这样吗:
create table 表a(部门序号 int,  部门名称 varchar(20))

insert into 表a
select   1        ,'办公室' union all
select   2        ,'人事处'

create table 表b(id  int,   姓名 varchar(20), 部门 int)

insert into 表b
select 1     ,'王x',   1 union all
select 2     ,'李x',   1 union all
select 3     ,'刘x',   2 union all
select 4     ,'张x',   2

create table 表c(序号 int,  员工id int, 得分 numeric(10,2))

insert into 表c
select 1      ,1,     2 union all
select 2      ,2,     3 union all
select 3      ,3,     5 union all
select 4      ,4,     2 union all
select 5      ,1,     2.5 union all
select 6      ,1,     1.3
go


select (select SUM(c.得分) * 1.0 / COUNT(distinct bb.id)
        from 表a a
        inner join 表b bb
                on a.部门序号 = bb.部门
        left join 表c c
               on c.员工id = bb.id         
        where a.部门序号 = b.部门)
from 表b b  
where b.id = 1
/*
4.400000
*/
ruhong 2014-01-04
  • 打赏
  • 举报
回复
办公室2个人,结果就是(2+3+2.5+1.3)/2=4.4
LongRui888 2014-01-04
  • 打赏
  • 举报
回复
引用 8 楼 ruhong 的回复:
我重新写一下条件。 表a 部门序号 部门名称 1 办公室 2 人事处 表b id 姓名 部门 1 王x 1 2 李x 1 3 刘x 2 4 张x 2 表c 序号 员工id 得分 1 1 2 2 2 3 3 3 5 4 4 2 5 1 2.5 6 1 1.3 求id=1,名为王x所在部门的平均得分。
你要的结果是什么,多少分
LongRui888 2014-01-04
  • 打赏
  • 举报
回复
引用 7 楼 ruhong 的回复:
[quote=引用 6 楼 yupeigu 的回复:] [quote=引用 3 楼 ruhong 的回复:] 如果已知一条件,就是一个员工的代码,比如id=1,如何查找他的部门平均分呢?
是这样吗:
create table 表a(部门序号 int,  部门名称 varchar(20))

insert into 表a
select   1        ,'办公室' union all
select   2        ,'人事处'

create table 表b(id  int,   姓名 varchar(20), 部门 int)

insert into 表b
select 1     ,'王x',   1 union all
select 2     ,'李x',   1 union all
select 3     ,'刘x',   2 union all
select 4     ,'张x',   2

create table 表c(序号 int,  员工id int, 得分 int)

insert into 表c
select 1      ,1,     2 union all
select 2      ,2,     3 union all
select 3      ,3,     5 union all
select 4      ,4,     2
go


select (select SUM(c.得分) * 1.0 / COUNT(*)
        from 表a a
        inner join 表b bb
                on a.部门序号 = bb.部门
        left join 表c c
               on c.员工id = bb.id         
        where a.部门序号 = b.部门)
from 表b b  
where b.id = 1
/*
2.500000000000
*/
[/quote] 考虑过一个员工多条得分记录了吗?[/quote] 哦 那计算的时候,是总分 / 人次 ,还是 / 人数 呢,前者不去重,后者去重?
ruhong 2014-01-04
  • 打赏
  • 举报
回复
我重新写一下条件。 表a 部门序号 部门名称 1 办公室 2 人事处 表b id 姓名 部门 1 王x 1 2 李x 1 3 刘x 2 4 张x 2 表c 序号 员工id 得分 1 1 2 2 2 3 3 3 5 4 4 2 5 1 2.5 6 1 1.3 求id=1,名为王x所在部门的平均得分。
ruhong 2014-01-04
  • 打赏
  • 举报
回复
引用 6 楼 yupeigu 的回复:
[quote=引用 3 楼 ruhong 的回复:] 如果已知一条件,就是一个员工的代码,比如id=1,如何查找他的部门平均分呢?
是这样吗:
create table 表a(部门序号 int,  部门名称 varchar(20))

insert into 表a
select   1        ,'办公室' union all
select   2        ,'人事处'

create table 表b(id  int,   姓名 varchar(20), 部门 int)

insert into 表b
select 1     ,'王x',   1 union all
select 2     ,'李x',   1 union all
select 3     ,'刘x',   2 union all
select 4     ,'张x',   2

create table 表c(序号 int,  员工id int, 得分 int)

insert into 表c
select 1      ,1,     2 union all
select 2      ,2,     3 union all
select 3      ,3,     5 union all
select 4      ,4,     2
go


select (select SUM(c.得分) * 1.0 / COUNT(*)
        from 表a a
        inner join 表b bb
                on a.部门序号 = bb.部门
        left join 表c c
               on c.员工id = bb.id         
        where a.部门序号 = b.部门)
from 表b b  
where b.id = 1
/*
2.500000000000
*/
[/quote] 考虑过一个员工多条得分记录了吗?
LongRui888 2014-01-04
  • 打赏
  • 举报
回复
引用 3 楼 ruhong 的回复:
如果已知一条件,就是一个员工的代码,比如id=1,如何查找他的部门平均分呢?
是这样吗:
create table 表a(部门序号 int,  部门名称 varchar(20))

insert into 表a
select   1        ,'办公室' union all
select   2        ,'人事处'

create table 表b(id  int,   姓名 varchar(20), 部门 int)

insert into 表b
select 1     ,'王x',   1 union all
select 2     ,'李x',   1 union all
select 3     ,'刘x',   2 union all
select 4     ,'张x',   2

create table 表c(序号 int,  员工id int, 得分 int)

insert into 表c
select 1      ,1,     2 union all
select 2      ,2,     3 union all
select 3      ,3,     5 union all
select 4      ,4,     2
go


select (select SUM(c.得分) * 1.0 / COUNT(*)
        from 表a a
        inner join 表b bb
                on a.部门序号 = bb.部门
        left join 表c c
               on c.员工id = bb.id         
        where a.部门序号 = b.部门)
from 表b b  
where b.id = 1
/*
2.500000000000
*/
ruhong 2014-01-04
  • 打赏
  • 举报
回复
引用 4 楼 OROCHIHeart 的回复:
[quote=引用 3 楼 ruhong 的回复:] 如果已知一条件,就是一个员工的代码,比如id=1,如何查找他的部门平均分呢?

select c.序号,c.员工id,b.姓名,b.部门 as 部门序号,a.部门名称,sum(c.得分)/COUNT(*) as 平均得分
from 表C c
left join 表B b on c.员工id = b.id
left join 表A a on b.部门 = a.部门序号
where c.员工id = 1
[/quote] 对不起,我条件没给全,我试了一下不对。 这是因为得分记录中,同一个员工,他不是只有一条记录,因此count(*)不对, 就是说所有的得分再除员工的个数,一旦同一个员工得分记录超过2条就不对了。
orochiheart 2014-01-04
  • 打赏
  • 举报
回复
引用 3 楼 ruhong 的回复:
如果已知一条件,就是一个员工的代码,比如id=1,如何查找他的部门平均分呢?

select c.序号,c.员工id,b.姓名,b.部门 as 部门序号,a.部门名称,sum(c.得分)/COUNT(*) as 平均得分
from 表C c
left join 表B b on c.员工id = b.id
left join 表A a on b.部门 = a.部门序号
where c.员工id = 1
ruhong 2014-01-04
  • 打赏
  • 举报
回复
如果已知一条件,就是一个员工的代码,比如id=1,如何查找他的部门平均分呢?
orochiheart 2014-01-04
  • 打赏
  • 举报
回复

select c.序号,c.员工id,b.姓名,b.部门 as 部门序号,a.部门名称,sum(c.得分)/COUNT(*) as 平均得分
from 表C c
left join 表B b on c.员工id = b.id
left join 表A a on b.部门 = a.部门序号
ruhong 2014-01-04
  • 打赏
  • 举报
回复
我验证看看。辛苦了。
LongRui888 2014-01-03
  • 打赏
  • 举报
回复
是这样吗:
create table 表a(部门序号 int,  部门名称 varchar(20))

insert into 表a
select   1        ,'办公室' union all
select   2        ,'人事处'

create table 表b(id  int,   姓名 varchar(20), 部门 int)

insert into 表b
select 1     ,'王x',   1 union all
select 2     ,'李x',   1 union all
select 3     ,'刘x',   2 union all
select 4     ,'张x',   2

create table 表c(序号 int,  员工id int, 得分 int)

insert into 表c
select 1      ,1,     2 union all
select 2      ,2,     3 union all
select 3      ,3,     5 union all
select 4      ,4,     2
go


select sum(c.得分)*1.0 / COUNT(*) as 平均得分
from 表a a
left join 表b b
       on a.部门序号 = b.部门
left join 表c c
       on c.员工id = b.id 
where a.部门名称 = '办公室'
/*
2.500000000000
*/

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧