create table #Stuff(
项目 varchar(20),
一月 decimal(8,2) default (0) ,
二月 decimal(8,2) default (0) ,
三月 decimal(8,2) default (0),
四月 decimal(8,2) default (0),
五月 decimal(8,2) default (0),
六月 decimal(8,2) default (0),
七月 decimal(8,2) default (0),
八月 decimal(8,2) default (0),
九月 decimal(8,2) default (0),
十月 decimal(8,2) default (0),
十一月 decimal(8,2) default (0),
十二月 decimal(8,2) default (0)
);
--班组在职人员统计临时表
create table #tmpStuff(
项目 varchar(20),
月份 varchar(20),
人数 int
);
--新员工当月离职
create table #tmpStuff2(
项目 varchar(20),
月份 varchar(20),
人数 int
);
--员工(不含当月)当月离职
create table #tmpStuff3(
项目 varchar(20),
月份 varchar(20),
人数 int
);
declare @year as char(4)
set @year='2015'
declare @yearmonth as char(6)
declare @currentmonthcount as int --当月(含当月)以前入职到目前还在职人员数量
declare @leavecount as int --当月(含当月)以前入职,当月之后离职人员数量
declare @i int
declare @01 int,@02 int,@03 int,@04 int,@05 int,@06 int,@07 int,@08 int,@09 int,@10 int,@11 int,@12 int --定义总人数变量
declare @01_lz int,@02_lz int,@03_lz int,@04_lz int,@05_lz int,@06_lz int,@07_lz int,@08_lz int,@09_lz int,@10_lz int,@11_lz int,@12_lz int --定义离职总人数变量
set @i=1
declare @month char(2)
while @i<=12
BEGIN
set @month=right(@i+100,2)
set @yearmonth =@year+right(@i+100,2)
if(@yearmonth<=CONVERT(char(6),getdate(),112))
begin
--统计当月(含当月)以前入职到目前还在职人员数量
insert into #tmpStuff(项目,月份,人数)select A01156 as 项目,@month as 月份,count(A0183) as 数量
from A01 where A0183='电子'
and A01155='生产'
and A01156<>'计划-仓储'
and A0179 is null
and convert(char(6) ,A0141,112)<=@yearmonth
group by A01156
--当月(含当月)以前入职,当月之后离职人员数量
insert into #tmpStuff(项目,月份,人数)select A01156 as 项目,@month as 月份,count(A0183) as 数量
from A01 where A0183='电子'
and A01155='生产'
and A01156<>'计划-仓储'
and convert(char(6) ,A0141,112)<=@yearmonth
and CONVERT(char(6),A0179,112)>@yearmonth
group by A01156
--非新员工当月离职统计
insert into #tmpStuff2(项目,月份,人数)
select case when A01158 in ( '个人发展','个人离职','个人 辞职','个人辞职',NULL,'申请离职','紧急辞职','不适应','辞职') then '辞职'
when A01158 is null then '辞职'
when A01158 in ('协议离职','合同期满') then '合同期满'
when A01158 in ('旷工自离','自动离职','自离') then '自离'
else A01158 end 项目,@month as 月份,count(A0183) as 数量
from A01 where A0183='电子'
and A01155='生产'
and A01156<>'计划-仓储'
and convert(char(6) ,A0141,112)<>@yearmonth
and CONVERT(char(6),A0179,112)=@yearmonth
group by A01158
--新员工当月离职统计
insert into #tmpStuff3(项目,月份,人数)select '新员工当月离职' as 项目,@month as 月份,count(A0183) as 数量
from A01 where A0183='电子'
and A01155='生产'
and A01156<>'计划-仓储'
and convert(char(6) ,A0141,112)=@yearmonth
and CONVERT(char(6),A0179,112)=@yearmonth
end
set @i=@i+1
set @currentmonthcount=0
set @leavecount=0
END
--行转列:项目 月份
select '04.员工总人数' as 项目,月份,sum(人数) as 人数 into #totalStuff from #tmpStuff group by 月份
--总人数存到变量
select @01=[01], @02=[02], @03=[03], @04=[04], @05=[05], @06=[06], @07=[07], @08=[08], @09=[09], @10=[10],@11=[11],@12=[12]
from #totalStuff pivot(max(人数) for 月份 in ([01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12])) f
--
select '离职总人数' as 项目, 月份,sum(人数) as 人数 into #totalStuffLz from (
select * from #tmpStuff3
union all
select * from #tmpStuff2
) m
group by m.月份
--离职总人数存到变量
select @01_lz=[01], @02_lz=[02], @03_lz=[03], @04_lz=[04], @05_lz=[05], @06_lz=[06], @07_lz=[07], @08_lz=[08], @09_lz=[09], @10_lz=[10],@11_lz=[11],@12_lz=[12]
from #totalStuffLz pivot(max(人数) for 月份 in ([01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12])) g
insert #Stuff(项目,一月,二月,三月,四月,五月,六月,七月 ,八月 ,九月 ,十月,十一月,十二月) select *
from #totalStuff pivot(max(人数) for 月份 in ([01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12])) b
insert #Stuff(项目,一月,二月,三月,四月,五月,六月,七月 ,八月 ,九月 ,十月,十一月,十二月)
--union all
select case when 项目='生产一线' then '01.生产一线'
when 项目='生产-背光' then '02.生产-背光'
when 项目='生