• 主页
• 基础类
• 应用实例
• 新技术前沿

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

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
``````
...全文
52 点赞 收藏 5

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 行）
*/``````

MS-SQL Server

3.2w+

MS-SQL Server相关内容讨论专区