27,580
社区成员
发帖
与我相关
我的任务
分享
select a.d_id,a.[unum] as [部门总人数],a.[savg] as [部门平均工资]
from Tdep b right join
(select d.d_id,isnull(s.[month],0) as [month],count(distinct(u.u_id)) as[unum],isnull(avg(s.salary),0) as [savg]
from Tdep d left join Tuser u
on d.d_id=u.d_id
left join Tsalary s
on u.u_id=s.u_id
group by d.d_id,s.[month]
) a
on b.d_id=a.d_id
order by a.d_id
d_id 部门总人数 部门平均工资
---------- ----------- -----------
01 2 1250
01 2 1750
01 2 2000
02 1 2500
02 1 2500
02 1 2500
03 3 1500
03 3 1700
03 3 1833
04 0 0
(所影响的行数为 10 行)
select d.d_id,isnull(s.[month],0) as [month],count(distinct(u.u_id)) as[unum],isnull(avg(s.salary),0) as [savg]
from Tdep d left join Tuser u
on d.d_id=u.d_id
left join Tsalary s
on u.u_id=s.u_id
group by d.d_id,s.[month]
order by d.d_id
d_id month unum savg
---------- ----- ----------- -----------
01 1 2 1250
01 2 2 1750
01 3 2 2000
02 1 1 2500
02 2 1 2500
02 3 1 2500
03 1 3 1500
03 2 3 1700
03 3 3 1833
04 0 0 0
(所影响的行数为 10 行)
SELECT a.[year] AS 工资年份, a.mouth AS 工资月份, c.d_id AS 部门编号, COUNT(*)
AS 人数, Avg(a.salary) AS 平均工资
FROM dbo.Tsalary a LEFT OUTER JOIN
dbo.Tuser b ON a.u_id = b.u_id LEFT OUTER JOIN
dbo.Tdep c ON b.d_id = c.d_id
GROUP BY c.d_id, a.[year], a.mouth
if object_id(N'[dbo].[Tsalary]') is not null
drop table [dbo].[Tsalary]
go
if object_id(N'[dbo].[Tuser]') is not null
drop table [dbo].[Tuser]
go
if object_id(N'[dbo].[Tdep]') is not null
drop table [dbo].[Tdep]
go
create table [dbo].[Tuser](u_id smallint primary key,d_id smallint,u_name varchar(50))
go
create table [dbo].[Tsalary](u_id smallint foreign key references [dbo].[Tuser]([u_id]), [year] smallint ,[month] smallint,salary smallint)
go
create table [dbo].[Tdep](d_id smallint primary key,d_name varchar(50))
go
insert [dbo].[Tuser]
select 1,1,'张三' union all
select 2,1,'王五' union all
select 3,2,'小照' union all
select 4,3,'小林' union all
select 5,3,'天天' union all
select 6,3,'呖呖' union all
select 7,null,'菲菲'
go
insert [dbo].[Tdep]
select 1,'技术部' union all
select 2,'财务部' union all
select 3,'市场部' union all
select 4,'企划部'
go
insert [dbo].[Tsalary]
select 1,2005,1,1000 union all
select 1,2005,2,1500 union all
select 1,2005,3,1500 union all
select 2,2005,1,1500 union all
select 2,2005,2,2000 union all
select 2,2005,3,2500 union all
select 3,2005,1,2500 union all
select 3,2005,2,2500 union all
select 3,2005,3,2500 union all
select 4,2005,1,1800 union all
select 4,2005,2,1900 union all
select 4,2005,3,2000 union all
select 5,2005,1,1500 union all
select 5,2005,2,1700 union all
select 5,2005,3,1800 union all
select 6,2005,1,1200 union all
select 6,2005,2,1500 union all
select 6,2005,3,1700
go
select 部门编号 = [dbo].[Tdep].[d_id],平均工资 = avg([dbo].[Tsalary].[salary]) from
[dbo].[Tuser] inner join [dbo].[Tdep] on [dbo].[Tuser].[d_id] = [dbo].[Tdep].[d_id]
inner join [dbo].[Tsalary] on [dbo].[Tuser].[u_id] = [dbo].[Tsalary].[u_id]
group by [dbo].[Tdep].[d_id]
1 1666
2 2500
3 1677
select 部门编号 = [d_id],年份 = [year],月份= [month],部门总人数 = count([u_id]),平均工资=avg([salary])
from
(select [Tuser].[u_id],[Tdep].[d_id],[Tsalary].[year],[Tsalary].[month],[Tsalary].[salary] from
[dbo].[Tuser] inner join [dbo].[Tdep] on [dbo].[Tuser].[d_id] = [dbo].[Tdep].[d_id]
inner join [dbo].[Tsalary] on [dbo].[Tuser].[u_id] = [dbo].[Tsalary].[u_id]) as a
group by [d_id],[year],[month]
go
部门编号 年份 月份 部门总人数 平均工资
1 2005 1 2 1250
1 2005 2 2 1750
1 2005 3 2 2000
2 2005 1 1 2500
2 2005 2 1 2500
2 2005 3 1 2500
3 2005 1 3 1500
3 2005 2 3 1700
3 2005 3 3 1833
Select a.d_id as '部门编号',Count(*) as '部门总人数',Sum(Salary)/Count(*) as '部门平均工资'
from TDep a inner join TUser b on a.d_id = b.d_id inner join TSalary c on b.u_id = c.u_id
Group by a.d_id