如何计算某月份中员工的平均金额?表结构已给出

chouqin681 2007-12-19 10:41:09
如何计算某月份中员工的平均金额?表结构已给出

create table #tmp (
月份 char(6),
日期 char(2),
姓名 varchar(10),
金额 numeric(18,2)
)

insert into #tmp
select '200701','02','张三',100 union all
select '200701','03','张三',500 union all
select '200701','04','王五',150 union all
select '200701','05','王五',400 union all
select '200702','02','张三',800 union all
select '200702','03','张三',900 union all
select '200702','04','王五',550 union all
select '200702','05','王五',200


--如何到得每个月份员工的平均金额

--200701的金额平均为:575
-- (100+500+150+400)/2 = 575
--因为只有张三和王五两个人,故除以2

--200702的金额平均为:1250
-- (800+900+550+200)/2=2450
...全文
123 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangCK 2007-12-19
  • 打赏
  • 举报
回复
create table #tmp (
月份 char(6),
日期 char(2),
姓名 varchar(10),
金额 numeric(18,2)
)

insert into #tmp
select '200701','02','张三',100 union all
select '200701','03','张三',500 union all
select '200701','04','王五',150 union all
select '200701','05','王五',400 union all
select '200702','02','张三',800 union all
select '200702','03','张三',900 union all
select '200702','04','王五',550 union all
select '200702','05','王五',200

select 月份,姓名,cast(sum(金额)/count(姓名) as decimal(18,2)) as 平均金额
from #tmp
group by 月份,姓名

drop table #tmp

/*
月份 姓名 平均金额
------ ---------- --------------------
200701 王五 275.00
200702 王五 375.00
200701 张三 300.00
200702 张三 850.00

(所影响的行数为 4 行)

*/
$扫地僧$ 2007-12-19
  • 打赏
  • 举报
回复



select 月份,sum(金额)/count(distinct 姓名)
from #tmp T
group by 月份
tim_spac 2007-12-19
  • 打赏
  • 举报
回复

create table #tmp (
月份 char(6),
日期 char(2),
姓名 varchar(10),
金额 numeric(18,2)
)
go
insert into #tmp
select '200701','02','张三',100 union all
select '200701','03','张三',500 union all
select '200701','04','王五',150 union all
select '200701','05','王五',400 union all
select '200702','02','张三',800 union all
select '200702','03','张三',900 union all
select '200702','04','王五',550 union all
select '200702','05','王五',200
go

--如何到得每个月份员工的平均金额
select 月份, 金额 = avg(金额)
from (
select 月份, 姓名, 金额 = sum(金额)
from #tmp
group by 月份, 姓名
) as a
group by 月份
go

drop table #tmp
go
liangCK 2007-12-19
  • 打赏
  • 举报
回复
create table #tmp (
月份 char(6),
日期 char(2),
姓名 varchar(10),
金额 numeric(18,2)
)

insert into #tmp
select '200701','02','张三',100 union all
select '200701','03','张三',500 union all
select '200701','04','王五',150 union all
select '200701','05','王五',400 union all
select '200702','02','张三',800 union all
select '200702','03','张三',900 union all
select '200702','04','王五',550 union all
select '200702','05','王五',200

select distinct a.月份,a.姓名,平均金额=cast(b.金额/b.人数 as decimal(10,2))
from #tmp a,(select 月份,姓名,金额=sum(金额),人数=count(姓名) from #tmp group by 月份,姓名) b
where a.月份=b.月份 and a.姓名=b.姓名

drop table #tmp

/*
月份 姓名 平均金额
------ ---------- ------------
200701 王五 275.00
200701 张三 300.00
200702 王五 375.00
200702 张三 850.00

(所影响的行数为 4 行)

*/
liangCK 2007-12-19
  • 打赏
  • 举报
回复
create table #tmp (
月份 char(6),
日期 char(2),
姓名 varchar(10),
金额 numeric(18,2)
)

insert into #tmp
select '200701','02','张三',100 union all
select '200701','03','张三',500 union all
select '200701','04','王五',150 union all
select '200701','05','王五',400 union all
select '200702','02','张三',800 union all
select '200702','03','张三',900 union all
select '200702','04','王五',550 union all
select '200702','05','王五',200

select distinct a.月份,平均金额=cast(b.金额*1.0/b.人数 as decimal(10,2)) from
#tmp a,(select 月份,人数=count(distinct 姓名),金额=sum(金额) from #tmp group by 月份) b
where a.月份=b.月份


drop table #tmp

/*
月份 平均金额
------ ------------
200701 575.00
200702 1225.00

(所影响的行数为 2 行)
*/

34,838

社区成员

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

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