有用存储过程

USE [ChiefMes_Kenta]
GO
/****** Object: UserDefinedFunction [dbo].[checkReValue] Script Date: 01/15/2016 10:32:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[checkReValue]
(
@old nvarchar(100),
@checkNew nvarchar(100)
)
RETURNS nvarchar(100)
--取出A3,A4的值
AS
begin
--declare @old nvarchar(100)='A01,A02,A03,A04';
--declare @checkNew nvarchar(100)='A01,A02';
-- select dbo.checkReValue('A01,A02,A03,A04,A05','A01,A02')
declare @returnValue nvarchar(100)=''

declare @duo nvarchar(100)='';
set @old=ltrim(rtrim(@old))
set @checkNew=ltrim(rtrim(@checkNew))

DECLARE @NextIndexOLD int;
declare @currentIndexOLD int=1
while(@currentIndexOLD<=len(@old))
begin
declare @returnValueOLD nvarchar(100);
select @NextIndexOLD=charindex(',',@old,@currentIndexOLD)
if(@NextIndexOLD=0 or @NextIndexOLD is null)
begin
select @NextIndexOLD=len(@old)+1
end
select @returnValueOLD=substring(@old,@currentIndexOLD,@NextIndexOLD-@currentIndexOLD)
if(charindex(@returnValueOLD,@checkNew)<=0)
begin
set @returnValue+=@returnValueOLD+',';

end
--查找是否在@checkNew出现
----------------------------------
-- begin
-- DECLARE @NextIndexNew int;
-- declare @currentIndexNew int=1
--while(@currentIndexNew<=len(@checkNew))
--begin
-- declare @returnValueNew nvarchar(100);
-- select @NextIndexNew=charindex(',',@checkNew,@currentIndexNew)
-- if(@NextIndexNew=0 or @NextIndexNew is null)
-- begin
-- select @NextIndexNew=len(@checkNew)+1
-- end
-- select @returnValueNew=substring(@checkNew,@currentIndexNew,@NextIndexNew-@currentIndexNew);
-- --(@returnValueNew=@returnValueOLD)
-- --select @returnValueNew
-- select @returnValueOLD
-- if(charindex(@returnValueOLD,@returnValueNew)<=0)
-- begin
-- set @returnValue+=@returnValueOLD+',';

-- end


--select @currentIndexNew=@NextIndexNew+1
--end

-- end
------------------------------------------------

select @currentIndexOLD=@NextIndexOLD+1
end

--select ltrim(rtrim(@returnValue))
--select @duo
set @returnValue=substring(@returnValue,0,len(@returnValue))
return ltrim(rtrim(@returnValue))

end

---------------------------------------------------------------------------------------------------------

USE [ChiefMes_Kenta]
GO
/****** Object: StoredProcedure [dbo].[Kenta_RptEmpOEE] Script Date: 01/15/2016 10:31:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/*************************************************
Creator: WuChun
Create Date: 2013-10-22
Remark: 计算健大员工OEE数据报表
*************************************************/
ALTER procedure [dbo].[Kenta_RptEmpOEE]
(
@GroupType varchar(30) = 'Sum' ,--Detail: 人员OEE明细 Sum:人员OEE汇总
@EmpGroupNO varchar(30) = '', --人员组别
@DateBegin varchar(10) = '2015-05-06',
@DateEnd varchar(10) = '2015-06-06',
@EmpID varchar(10) = '',
@BC varchar(10) = ''
)

as
declare @ProductName varchar(100), @MachineNo varchar(30), @BCCode varchar(10)
if object_id('tempdb..#DailyTableEmp') is not null drop table #DailyTableEmp
create table #DailyTableEmp
(
MachineNo varchar(30),--机器编号
EmpGroupNO varchar(50),
EmpID varchar(50),--员工工号
CurrDate varchar(10),--日期
BCCode varchar(10),--班次
DispatchNo varchar(30),--派工单号
TZ_CustomerNo varchar(30),--需求客户
MouldNo varchar(30),
ProductName varchar(2000),--产品名称
ProductNo varchar(30),--产品编号
TZ_MONO varchar(30),--制令单号
DispatchNum int,--派工数量
ProductNum int,--产品种数
StandSocketNum int,--标准出数
SocketNum int,--实际出数
SocketRate decimal(10,2),--出数率
SumNormalMouldNum int,--总的模次数
SumNormalCycle int,--人工周期总和
StandCycle decimal(10,2),--标准周期
AverageCycle decimal(10,2),--实际平均周期
CycleDiffRate decimal(10,2),--平均周期差异率(周期百分比=(AverageCycle-StandCycle)/StandCycle)
TZ_StandEmp decimal(10,2),--标准人数
ActualEmp decimal(10,2),--实际人数
----至此为派工单信息段
DispatchTime decimal(10,2),--派工单总时间
PlanProductTime decimal(10,2),--计划生产时间
RealMacTime decimal(10,2),--实际稼动时间
NoPlanTime decimal(10,2),--计划停机时间
HuanMo decimal(10,2),--换模时间
HuanLiao decimal(10,2),--换料时间
HuanDan decimal(10,2),--换单时间
JiQiGuZhang decimal(10,2),--机器故障时间
MoJuGuZhang decimal(10,2),--模具故障时间
FuSheGuZhang decimal(10,2),--辅设故障时间
DaiLiao decimal(10,2),--待料时间
WuDingDan decimal(10,2),--无订单时间
QiTa decimal(10,2),--在线保养时间
DaiRen decimal(10,2),--原材料不良时间
MacIdleTime decimal(10,2),--待机空闲时间
PermissionTime decimal(10,2) default(0.00),--批准时间
----至此为停机时间信息段
PlanOutputQty int,--计划产量=(总时间-计划停机时间)*3600/标准周期*标准出数
ActualOutputQty int,--实际产量
PackageNum int,--包装数
TheoreticalQty int,--理论实际生产时间产能=实际稼动时间*3600/标准周期*标准出数
ProductedNum int,--已生产数
BadNum int,--不良品数
BadRate decimal(10,4),--不良率
EfficiencyRate decimal(10,4),--机台效率(有效率)=生产总数/理论实际生产时间产能
Availability decimal(10,4),--有效开机率(表现性,有效性)=实际稼动时间/(总时间-计划停机时间-批准时间)
OEE decimal(10,4),--=机台效率*有效开机率*良率
Area varchar(1000),--负责区域
Flag int--层级标志,0派工单级别(有生产),1派工单级别(无生产),2机器编号级别,3人员汇总级别
)

if object_id('tempdb..#PlanTime') is not null drop table #PlanTime
create table #PlanTime
(
MachineNo varchar(30),
PlanProductTime decimal(10,2),
BCCode varchar(10)
)


--20150527
if OBJECT_ID('tempdb..#leixinArea') is not null drop table #leixinArea
create table #leixinArea
(
MachineGroup nvarchar(50),
ID char(10)
)
select A.MachineGroup,A.ID into #a from Kenta_EmpOEEConfig A
declare @MachineGroup nvarchar(50),@ID char(10)

DECLARE cursor_outer CURSOR FOR
select m.MachineGroup,m.ID from #a m
OPEN cursor_outer
fetch next from cursor_outer into @MachineGroup,@ID
while @@FETCH_STATUS=0
begin
insert into #leixinArea select fstt.Col,@ID from dbo.FN_SplitSTR(@MachineGroup,',') fstt
fetch next from cursor_outer into @MachineGroup,@ID
end
close cursor_outer
deallocate cursor_outer
--20150527


--20150528CJ_start02 1.先将表Kenta_EmpOEEAreaConfig 生成 #CJ 退换有临时情况的
select Y.* into #CJ from Kenta_EmpOEEAreaConfig Y order by MachineGroup,ID
--if(Exists(select * from Kenta_EmpOEEConfig A where A.MachineGroup='临时'))
if(Exists(
select A.*,B.*,Y.* from Kenta_EmpOEEConfig A
join #leixinArea B on A.ID=B.ID
join Kenta_EmpOEEAreaConfig Y on Y.MachineGroup=B.MachineGroup
where Y.Flag='L'
)
)
begin
print 'L'

update ss set ss.MachineNo=dbo.checkReValue(ss.MachineNo,TT.MachineNo)
from #CJ ss, #CJ TT
where
TT.startDate>ss.startDate
and (TT.endDate<ss.endDate or ss.endDate is null)
and TT.Flag='L'--临时


--select dbo.checkReValue(ss.MachineNo,TT.MachineNo),ss.MachineNo,TT.MachineNo
--from #CJ ss, #CJ TT
--where
--TT.startDate>ss.startDate
--and (TT.endDate<ss.endDate or ss.endDate is null)
--and TT.Flag='L'--临时

end
else
begin
print 'G'
end

--20150528CJ_end02

--select Y.* from Kenta_EmpOEEAreaConfig Y order by MachineGroup,ID
--select * from #CJ


--20150527CJ_start02 2.再将#CJ进行分割之生成表 #AreaConfig
if OBJECT_ID('tempdb..#AreaConfig') is not null drop table #AreaConfig
create table #AreaConfig
(
MachineNo nvarchar(50),
ID char(10)
)
--select z.MachineNo,z.ID into #kk from Kenta_EmpOEEAreaConfig z
select z.MachineNo,z.ID into #kk from #CJ z
declare @MachineNoA nvarchar(50),@IDA char(10)
DECLARE cursor_outerA CURSOR FOR
select m.MachineNo,m.ID from #kk m
OPEN cursor_outerA

fetch next from cursor_outerA into @MachineNoA,@IDA
while @@FETCH_STATUS=0
begin
insert into #AreaConfig select fstt.Col,@IDA from dbo.FN_SplitSTR(@MachineNoA,',') fstt
fetch next from cursor_outerA into @MachineNoA,@IDA
end
close cursor_outerA
deallocate cursor_outerA
--20150527CJ_end02


insert into #DailyTableEmp(MachineNo,EmpGroupNO, EmpID, CurrDate,BCCode,DispatchNo,TZ_CustomerNo,MouldNo,ProductName,
ProductNo,TZ_MONO,DispatchNum,ProductNum,StandSocketNum,SocketNum,SocketRate,SumNormalMouldNum,SumNormalCycle,
StandCycle,AverageCycle,CycleDiffRate,TZ_StandEmp,ActualEmp,DispatchTime, PlanProductTime,RealMacTime,NoPlanTime,
HuanMo,HuanLiao,HuanDan,JiQiGuZhang,MoJuGuZhang,FuSheGuZhang,DaiLiao,WuDingDan,QiTa,DaiRen,MacIdleTime,PermissionTime,
PlanOutputQty,ActualOutputQty,PackageNum,TheoreticalQty,ProductedNum,BadNum,BadRate,EfficiencyRate,Availability,OEE,Flag)
select
p.MachineNo,e.EmpGroupNO, e.EmpID, p.CurrDate,p.BCCode,p.DispatchNo,p.TZ_CustomerNo,p.MouldNo,p.ProductName,p.ProductNo,
p.TZ_MONO,p.DispatchNum,p.ProductNum,p.StandSocketNum,p.SocketNum,p.SocketRate,p.SumNormalMouldNum,p.SumNormalCycle,
p.StandCycle,p.AverageCycle,p.CycleDiffRate,p.TZ_StandEmp,p.ActualEmp,p.DispatchTime, p.PlanProductTime,p.RealMacTime,
p.NoPlanTime,p.HuanMo,p.HuanLiao,p.HuanDan,p.JiQiGuZhang,p.MoJuGuZhang,p.FuSheGuZhang,p.DaiLiao,p.WuDingDan,p.QiTa,
p.DaiRen,p.MacIdleTime,p.PermissionTime,p.PlanOutputQty,p.ActualOutputQty,p.PackageNum,p.TheoreticalQty,
p.ProductedNum,p.BadNum,p.BadRate,p.EfficiencyRate,p.Availability,p.OEE,p.Flag
from Kenta_DailyProductionInfo p

join Kenta_EmpOEEConfig e
on
(@EmpGroupNO = '' or e.EmpGroupNO = @EmpGroupNO)
--and charindex(p.MachineNo,e.MachineNo)>0
and p.BCCode=e.BCCode --班次
and e.ShowOEE=1
and ((p.CurrDate between e.startDate and e.EndDate) or (p.CurrDate>=e.startDate and e.EndDate is null))

--20150527CJ S
join #leixinArea B
on e.ID=B.ID

--join Kenta_EmpOEEAreaConfig C--wo jia 人员区域时段配置表
--20150528s
join #CJ C
--20150528E
on C.MachineGroup=B.MachineGroup
and ((p.CurrDate between C.startDate and C.endDate) or (p.CurrDate>=C.startDate and C.EndDate is null))

join #AreaConfig T
on C.ID=T.ID and p.MachineNo=T.MachineNo
--20150527CJ E



where p.CurrDate between @DateBegin and @DateEnd
and DispatchNo<>''
and (@EmpID = '' or e.EmpID = @EmpID or e.EmpName like @EmpID +'%')
order by p.CurrDate, p.BCCode, p.MachineNo

--20150602ST
--select '这里是:Kenta_EmpOEEConfig 下'
--select * from Kenta_EmpOEEConfig

--select '这里是#leixinArea 下'
--select * from #leixinArea

--select '这里是#CJ 下'
--select * from #CJ

--select '这里是#AreaConfig 下'
--select * from #AreaConfig

--select * from Kenta_EmpOEEConfig
--select '这里是#DailyTableEmp 下'
--select * from #DailyTableEmp
--end


update a set a.PlanProductTime=b.PlanProductTime
from #DailyTableEmp a
inner join #PlanTime b on a.MachineNo=b.MachineNo and a.BCCode=b.BCCode

update #DailyTableEmp set SocketRate=case when StandSocketNum=0 then 0 else SocketNum/(StandSocketNum*1.0) end--出数率
, BadRate=case when ProductedNum=0 then 0 else BadNum/(ProductedNum*1.0) end--不良率
, EfficiencyRate=case when TheoreticalQty=0 then 0 else ProductedNum/(TheoreticalQty*1.0) end--机台效率(有效率)
--, Availability=case when DispatchTime-NoPlanTime=0 then 0 else RealMacTime/(DispatchTime-NoPlanTime-PermissionTime) end--有效开机率(表现性,有效性)
, Availability=case when DispatchTime-NoPlanTime-PermissionTime-DaiRen=0 then 0 else RealMacTime/(DispatchTime-NoPlanTime-PermissionTime-DaiRen) end--有效开机率(表现性,有效性)
where Flag=2

update #DailyTableEmp set OEE=EfficiencyRate*Availability*(1-BadRate)--机台效率*有效开机率*良率
where Flag=2

--派工单层的OEE导入完成
--==================================================================================================================================

insert into #PlanTime
select distinct MachineNo, PlanProductTime, BCCode from #DailyTableEmp where CurrDate between @DateBegin and @DateEnd

insert into #DailyTableEmp(MachineNo,EmpGroupNO, EmpID, CurrDate, BCCode, StandSocketNum, SocketNum, TZ_StandEmp, ActualEmp, DispatchTime, RealMacTime, NoPlanTime, HuanMo, HuanLiao, HuanDan, JiQiGuZhang, MoJuGuZhang, FuSheGuZhang, DaiLiao, WuDingDan, QiTa, DaiRen, MacIdleTime, PermissionTime, PlanOutputQty, ActualOutputQty, PackageNum, TheoreticalQty, ProductedNum, BadNum, Flag)
select MachineNo, EmpGroupNO, EmpID, CurrDate, BCCode, SUM(StandSocketNum), SUM(SocketNum), SUM(TZ_StandEmp), SUM(ActualEmp), SUM(DispatchTime/ProductNum), SUM(RealMacTime/ProductNum), SUM(NoPlanTime/ProductNum), SUM(HuanMo/ProductNum), SUM(HuanLiao/ProductNum), SUM(HuanDan/ProductNum), SUM(JiQiGuZhang/ProductNum), SUM(MoJuGuZhang/ProductNum), SUM(FuSheGuZhang/ProductNum), SUM(DaiLiao/ProductNum), SUM(WuDingDan/ProductNum), SUM(QiTa/ProductNum), SUM(DaiRen/ProductNum), SUM(MacIdleTime/ProductNum), SUM(PermissionTime/ProductNum), SUM(PlanOutputQty), SUM(ActualOutputQty), SUM(PackageNum), SUM(TheoreticalQty), SUM(ProductedNum), SUM(BadNum),2
from #DailyTableEmp
where Flag in(0,1)
group by MachineNo,EmpGroupNO, EmpID, CurrDate, BCCode


update a set a.PlanProductTime=b.PlanProductTime from #DailyTableEmp a inner join #PlanTime b on a.MachineNo=b.MachineNo and a.BCCode=b.BCCode

update #DailyTableEmp set SocketRate=case when StandSocketNum=0 then 0 else SocketNum/(StandSocketNum*1.0) end--出数率
, BadRate=case when ProductedNum=0 then 0 else BadNum/(ProductedNum*1.0) end--不良率
, EfficiencyRate=case when TheoreticalQty=0 then 0 else ProductedNum/(TheoreticalQty*1.0) end--机台效率(有效率)
--, Availability=case when DispatchTime-NoPlanTime=0 then 0 else RealMacTime/(DispatchTime-NoPlanTime-PermissionTime) end--有效开机率(表现性,有效性)
, Availability=case when DispatchTime-NoPlanTime-PermissionTime-DaiRen=0 then 0 else RealMacTime/(DispatchTime-NoPlanTime-PermissionTime-DaiRen) end--有效开机率(表现性,有效性)
where Flag=2
update #DailyTableEmp set OEE=EfficiencyRate*Availability*(1-BadRate)--机台效率*有效开机率*良率
where Flag=2
--按机器编号层的OEE至此计算完成
--补充需求2013-05-27要求将所有派工单的产品名称用分号隔开对应到每台机器
--declare @rEmpGroupNO varchar(50), @rEmpNO varchar(50)
--declare cur_Daily cursor for
--select MachineNo, BCCode, ProductName, EmpGroupNO, EmpId from #DailyTableEmp where Flag in(0,1) and isnull(ProductName,'')<>''
--open cur_Daily
--fetch next from cur_Daily into @MachineNo, @BCCode, @ProductName, @rEmpGroupNO, @rEmpNO
--while @@FETCH_STATUS=0
--begin
-- print @MachineNo+ @BCCode+@ProductName
-- update #DailyTableEmp set ProductName=isnull(ProductName,'')+@ProductName+'; '
-- where Flag=2 and MachineNo=@MachineNo and BCCode=@BCCode and EmpGroupNO = @rEmpGroupNO and EmpID = @rEmpNO

-- fetch next from cur_Daily into @MachineNo, @BCCode, @ProductName, @rEmpGroupNO, @rEmpNO
--end
--close cur_Daily
--deallocate cur_Daily
--==================================================================================================================================

insert into #DailyTableEmp(EmpGroupNO,EmpID,CurrDate, BCCode, StandSocketNum, SocketNum, TZ_StandEmp, ActualEmp, DispatchTime, PlanProductTime, RealMacTime, NoPlanTime, HuanMo, HuanLiao, HuanDan, JiQiGuZhang, MoJuGuZhang, FuSheGuZhang, DaiLiao, WuDingDan, QiTa, DaiRen, MacIdleTime, PermissionTime, PlanOutputQty, ActualOutputQty, PackageNum, TheoreticalQty, ProductedNum, BadNum,Area, Flag)
select a.EmpGroupNO, a.EmpID, CurrDate, BCCode, SUM(StandSocketNum), SUM(SocketNum), SUM(TZ_StandEmp), SUM(ActualEmp), SUM(DispatchTime), SUM(PlanProductTime), SUM(RealMacTime), SUM(NoPlanTime), SUM(HuanMo), SUM(HuanLiao), SUM(HuanDan), SUM(JiQiGuZhang), SUM(MoJuGuZhang), SUM(FuSheGuZhang), SUM(DaiLiao), SUM(WuDingDan), SUM(QiTa), SUM(DaiRen), SUM(MacIdleTime), SUM(PermissionTime), SUM(PlanOutputQty), SUM(ActualOutputQty), SUM(PackageNum), SUM(TheoreticalQty), SUM(ProductedNum), SUM(BadNum),
'', 3
from #DailyTableEmp a inner join MachineMstr b on a.MachineNo=b.Machine_Code
where Flag=2
group by a.EmpGroupNO, a.EmpID, CurrDate, BCCode


insert into #DailyTableEmp(EmpGroupNO,EmpID,CurrDate, BCCode, StandSocketNum, SocketNum, TZ_StandEmp, ActualEmp, DispatchTime, PlanProductTime, RealMacTime, NoPlanTime, HuanMo, HuanLiao, HuanDan, JiQiGuZhang, MoJuGuZhang, FuSheGuZhang, DaiLiao, WuDingDan, QiTa, DaiRen, MacIdleTime, PermissionTime, PlanOutputQty, ActualOutputQty, PackageNum, TheoreticalQty, ProductedNum, BadNum, Flag)
select '汇总',EmpID,'', '', SUM(StandSocketNum), SUM(SocketNum), SUM(TZ_StandEmp), SUM(ActualEmp), SUM(DispatchTime), SUM(PlanProductTime), SUM(RealMacTime), SUM(NoPlanTime), SUM(HuanMo), SUM(HuanLiao), SUM(HuanDan), SUM(JiQiGuZhang), SUM(MoJuGuZhang), SUM(FuSheGuZhang), SUM(DaiLiao), SUM(WuDingDan), SUM(QiTa), SUM(DaiRen), SUM(MacIdleTime), SUM(PermissionTime), SUM(PlanOutputQty), SUM(ActualOutputQty), SUM(PackageNum), SUM(TheoreticalQty), SUM(ProductedNum), SUM(BadNum),3
from #DailyTableEmp a inner join MachineMstr b on a.MachineNo=b.Machine_Code
where Flag=2
group by EmpGroupNO, a.EmpID

update #DailyTableEmp set SocketRate=case when StandSocketNum=0 then 0 else SocketNum/(StandSocketNum*1.0) end--出数率
, BadRate=case when ProductedNum=0 then 0 else BadNum/(ProductedNum*1.0) end--不良率
, EfficiencyRate=case when TheoreticalQty=0 then 0 else ProductedNum/(TheoreticalQty*1.0) end--机台效率(有效率)
, Availability=case when DispatchTime-NoPlanTime-PermissionTime-DaiRen=0 then 0 else RealMacTime/(DispatchTime-NoPlanTime-PermissionTime-DaiRen) end--有效开机率(表现性,有效性)
--, Availability=case when PlanProductTime=0 then 0 else RealMacTime/(PlanProductTime-PermissionTime) end--有效开机率(表现性,有效性)
where Flag=3
update #DailyTableEmp set OEE=EfficiencyRate*Availability*(1-BadRate)--机台效率*有效开机率*良率
where Flag=3
--按人员层级的OEE至此计算完成

update #DailyTableEmp set StandSocketNum=null,SocketNum=null,SocketRate=null,TZ_StandEmp=null,ActualEmp=null,DispatchTime=null,
PlanProductTime=null,EfficiencyRate=null,Availability=null,BadNum=null,BadRate=null where EmpGroupNO='汇总'
--==================================================================================================================================


if @GroupType='Detail'
begin
select t.MachineNo,t.EmpID,CurrDate,case when t.BCCode='A' then '白班' when t.BCCode='B' then '晚班' else '' end as BCCode,DispatchNo,TZ_CustomerNo,ProductName,ProductNo,TZ_MONO,DispatchNum,StandSocketNum,SocketNum
,convert(varchar(10), CAST(SocketRate*100 as decimal(10,0)))+'%' as SocketRate,StandCycle,AverageCycle
,convert(varchar(10), CAST(CycleDiffRate*100 as decimal(10,0)))+'%' as CycleDiffRate,TZ_StandEmp,ActualEmp,DispatchTime, PlanProductTime,RealMacTime,NoPlanTime,HuanMo,HuanLiao,HuanDan,JiQiGuZhang,MoJuGuZhang,FuSheGuZhang,DaiLiao,WuDingDan,QiTa,DaiRen,MacIdleTime,PermissionTime,PlanOutputQty,ActualOutputQty,PackageNum,TheoreticalQty
, EfficiencyRate,Availability = isnull(Availability,0),BadNum,BadRate,OEE = ISNULL(OEE,0)
, EmpGroupName =(select top 1 EmpGroupName from MESAlarm.dbo.AlarmEmpGroup g where g.EmpGroupNo = t.EmpGroupNO)
, EmployeeName = e.EmpName
from #DailyTableEmp t
left join Kenta_EmpOEEConfig e on t.EmpID = e.EmpID
where t.Flag=2 and t.BCCode like '%'+@BC+'%'
order by t.CurrDate, t.MachineNo, t.BCCode, t.DispatchNo
end
else if @GroupType='Sum'
begin
select t.EmpGroupNO,
case when t.EmpGroupNO='汇总' then '' else t.EmpID end EmpID,
t.EmpID EmpID2, CurrDate,
case when t.BCCode='A' then '白班' when t.BCCode='B' then '晚班' else '' end as BCCode,
DispatchNo,TZ_CustomerNo,ProductName,ProductNo,TZ_MONO,DispatchNum,StandSocketNum,SocketNum
,convert(varchar(10), CAST(SocketRate*100 as decimal(10,0)))+'%' as SocketRate,StandCycle,AverageCycle
,convert(varchar(10), CAST(CycleDiffRate*100 as decimal(10,0)))+'%' as CycleDiffRate,TZ_StandEmp,ActualEmp,DispatchTime,
PlanProductTime,RealMacTime,NoPlanTime,HuanMo,HuanLiao,HuanDan,JiQiGuZhang,MoJuGuZhang,FuSheGuZhang,DaiLiao,WuDingDan,
QiTa,DaiRen,MacIdleTime,PermissionTime,PlanOutputQty,ActualOutputQty,PackageNum,TheoreticalQty
,EfficiencyRate,Availability,BadNum,
case when t.EmpGroupNO='汇总' then '汇总' else convert(varchar(10),BadRate) end BadRate,
OEE,

--dbo.GetMachineArea(e.MachineNo) Area,
e.MachineGroup Area,

case when t.EmpGroupNO='汇总' then '' else t.EmpGroupNo end EmpGroupName
, EmployeeName = case when t.EmpGroupNO='汇总' then '' else e.EmpName end
from #DailyTableEmp t
left join Kenta_EmpOEEConfig e on t.EmpID = e.EmpID
-- 2015-06-02
-- and ((t.CurrDate between e.startDate and e.EndDate) or (t.CurrDate>e.startDate and e.EndDate is null))
and ((t.CurrDate between e.startDate and e.EndDate) or (t.CurrDate>=e.startDate and e.EndDate is null))
where t.Flag=3 and t.BCCode like '%'+@BC+'%'
order by EmpID2, case when t.EmpGroupNO='汇总' then 2 else 1 end,t.CurrDate, t.MachineNo, BCCode, DispatchNo
end

drop table #DailyTableEmp
--2015-05-28CJ
drop table #CJ
drop table #AreaConfig

 --------------------------------------------------------------------------------------------------------------

create date: 2015.03.05
Remark: 人员OEE区域配置 Kenta_InsertEmpOEEAreaConfig

************************************************/
ALTER PROCEDURE [dbo].[Kenta_InsertEmpOEEAreaConfig]
(
@MachineGroup varchar(200),
@MachineNo varchar(1000),
@CreateDate datetime,
@CreateBy varchar(50),
@startDate varchar(50),
--2015-05-29
@endDate varchar(50),
@Flag varchar(10)
)
AS
--更新上一个结束时间
if(@Flag='G') --固定
begin
update A
set endDate=DATEADD(dd,-1,@startDate)
from Kenta_EmpOEEAreaConfig A
join
(
select * from Kenta_EmpOEEAreaConfig P
where P.ID=(select MAX(ID) from Kenta_EmpOEEAreaConfig a
where a.MachineGroup=P.MachineGroup)
)B
on A.ID=B.ID
where A.MachineGroup=@MachineGroup

insert into Kenta_EmpOEEAreaConfig(MachineGroup,MachineNo,CreateDate,CreateBy,startDate,Flag)
values(@MachineGroup,@MachineNo,@CreateDate,@CreateBy,@startDate,@Flag)
end

else --临时的
begin

insert into Kenta_EmpOEEAreaConfig(MachineGroup,MachineNo,CreateDate,CreateBy,startDate,endDate,Flag)
values(@MachineGroup,@MachineNo,@CreateDate,@CreateBy,@startDate,@endDate,@Flag)
end

原文地址:https://www.cnblogs.com/chengjun/p/5133793.html