Mes首检确认统计的存储过程

USE [ChiefmesNEW]
GO
/****** Object: StoredProcedure [dbo].[st_MES_RptInspectFirstCollect] Script Date: 04/15/2016 18:14:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[st_MES_RptInspectFirstCollect]
(
@ItemNo varchar(50)='',
@ItemName varchar(200)='',
@CustNo varchar(10)='',
@DeptCode varchar(50)='',
@StartTime varchar(19)='2016-04-15',
@EndTime varchar(19)='',
@in_pagesize smallint =20000,
@in_page smallint = 1,
@out_total_rows int = 1 output
)
AS
BEGIN
/*创建临时表存储按日期、班次、物料(Date,BC,dItemNo)分组数据
此表数据包含CheckResult in(OK,NG)数据
*/
create table #t_ResultInspect
(
ID int,
Date varchar(10),
ItemNo varchar(50),
CustNo varchar(10),
BC char(20),
CheckReason varchar(50),
NGCount int,
OKCount int,
InspectCount int,
NGItemList varchar(4000),
AbnormalNO varchar(50)
)
create table #t_BillNoList
(
ItemNo varchar(50),
BC char(20),
CustNo varchar(10),
Date varchar(10),
AbnormalNO varchar(50),
NGItemList varchar(4000)
)

/*按日期、班次、物料、检验结果、首件原因分组数据存储临时表*/
declare @BCMinTime varchar(5)
select @BCMinTime=min(BeginTime) from MES_BCDetail where NextDay=0
select ID=identity(int),MonthName=convert(varchar(2),MONTH(d.dCreateTime))+'月',Station='904'
,d.DeptCode,s.DeptName,d.BC,dCheckResult,BillNO,AbnormalNO,CustNO
--,Date=convert(varchar(10),d.dCreateTime,120)
,Date=case when convert(datetime,SUBSTRING(convert(varchar(16),d.dCreateTime,120),12,5))<convert(datetime,@BCMinTime)
then CONVERT(varchar(10),dateadd(day,-1,d.dCreateTime),120)
else CONVERT(varchar(10),d.dCreateTime,120) end
,d.dItemNo,d.dCreateTime,ItemDesc=m.ItemName,CheckReason
into #t_BCInspect
from
(select dItemNo=p.ItemNO,BillNO,AbnormalNO,dCreateTime=p.CreateTime,dDispatchNo=p.DispatchNo
,dDispatchPrior=p.DispatchPrior,BC=[dbo].[Fn_GetBCCodeByTime](convert(varchar(19),p.CreateTime,120)),dCheckResult=p.CheckResult,p.CheckReason
,DeptCode=dbo.FN_GetSwingCardEmpNameByDis(p.DispatchNo,p.DispatchPrior,convert(varchar(19),p.CreateTime,120),'1')
from MES_Inspect p
where p.CheckType=2 and p.CheckResult<>'' and p.CheckReason<>'' --1.查询出班次
)d --2.按照时间、班次、物料分组、判定结果、首检原因
join MES_Item m on d.dItemNo=m.ItemNO
left join Sys_Department s on s.DeptCode=d.DeptCode
where (@ItemNo='' or d.dItemNo=@ItemNo)
and (@ItemName='' or m.ItemName like '%'+@ItemName+'%')
and (@CustNo='' or m.CustNO=@CustNo)
and (@DeptCode='' or s.DeptCode=@DeptCode)
and (@StartTime='' or convert(varchar(19),d.dCreateTime,120)>=@StartTime+' 08:00')
and (@EndTime='' or CONVERT(varchar(19),d.dCreateTime,120)<=convert(varchar(10),dateadd(dd,1,convert(datetime,@EndTime,120)),120) +' 08:00')
order by dCreateTime

insert into #t_ResultInspect(ID,Date,BC,CustNo,CheckReason,OKCount,NGCount,ItemNo)
select Max(ID),Date,BC,CustNO,CheckReason,sum(case when dCheckResult='OK' then 1 else 0 end),sum(case when dCheckResult='NG' then 1 else 0 end),
dItemNo from #t_BCInspect t
group by Date,BC,dItemNo,CheckReason,CustNo

insert into #t_BillNoList(Date,BC,ItemNo,CustNo,NGItemList,AbnormalNO)
select Date,BC,dItemNo,CustNo, STUFF((
SELECT ','+ BillNo FROM #t_BCInspect a WHERE t.Date = a.Date and t.bc=a.bc and t.dItemNo=a.dItemNo FOR XML PATH('')
),1 ,1, ''),
STUFF((
SELECT ','+ AbnormalNO FROM #t_BCInspect a WHERE t.Date = a.Date and t.bc=a.bc and t.dItemNo=a.dItemNo FOR XML PATH('')
),1 ,1, '') from #t_BCInspect t
group by Date,BC,dItemNo,CustNo

-- --select * from #t_ResultInspect

update t set InspectCount=NGCount+OKCount,t.NGItemList=dbo.[FN_GetNGItemByNo](b.NGItemList),t.AbnormalNO=b.AbnormalNO
from #t_ResultInspect t join #t_BillNoList b on t.Date = b.Date and t.bc=b.bc and t.ItemNo=b.ItemNo

select seq=identity(int),MonthName=convert(varchar(2),MONTH(c.Date))+'月',Station='904',ItemDesc=c.ItemDesc
,DeptName=c.DeptName,c.DeptCode,c.Date,c.BC,c.dItemNo,r.NGItemList
,r.NGCount1,r.NGCount2,r.NGCount3,r.NGCount4,r.NGCount5,r.NGCount6,r.NGCount7,r.NGCount8
,r.OKCount1,r.OKCount2,r.OKCount3,r.OKCount4,r.OKCount5,r.OKCount6,r.OKCount7,r.OKCount8
,NGCount=r.NGCount1+r.NGCount2+r.NGCount3+r.NGCount4+r.NGCount5+r.NGCount6+r.NGCount7+r.NGCount8
,OKCount=r.OKCount1+r.OKCount2+r.OKCount3+r.OKCount4+r.OKCount5+r.OKCount6+r.OKCount7+r.OKCount8
,InspectCount=r.NGCount1+r.NGCount2+r.NGCount3+r.NGCount4+r.NGCount5+r.NGCount6+r.NGCount7+r.NGCount8
+r.OKCount1+r.OKCount2+r.OKCount3+r.OKCount4+r.OKCount5+r.OKCount6+r.OKCount7+r.OKCount8
,rate=CONVERT(varchar(6),case when (r.OKCount1+r.OKCount2+r.OKCount3+r.OKCount4+r.OKCount5+r.OKCount6+r.OKCount7+r.OKCount8)=0 then 1
else
convert(decimal(18,2),(r.OKCount1+r.OKCount2+r.OKCount3+r.OKCount4+r.OKCount5+r.OKCount6+r.OKCount7+r.OKCount8)
/convert(float,(r.NGCount1+r.NGCount2+r.NGCount3+r.NGCount4+r.NGCount5+r.NGCount6+r.NGCount7+r.NGCount8+r.OKCount1+r.OKCount2+r.OKCount3+r.OKCount4+r.OKCount5+r.OKCount6+r.OKCount7+r.OKCount8))*100.00) end)+'%'
,r.AbnormalNO,CustNo
into #t_PageFirstCollect
from (select BC,Date,dItemNo,CustNo,ItemDesc=MAX(ItemDesc),ID=MAX(ID),DeptName=MAX(DeptName),DeptCode=MAX(DeptCode)
from #t_BCInspect
group by BC,Date,dItemNo,CustNo) c
join (select Date,BC,ItemNo,NGItemList=MAX(NGItemList),AbnormalNO=MAX(AbnormalNO)
,MAX(case CheckReason when '1-接班首检' then NGCount else 0 end) 'NGCount1'
,MAX(case CheckReason when '1-接班首检' then OKCount else 0 end) 'OKCount1'
,MAX(case CheckReason when '2-某批计划开始生产' then NGCount else 0 end) 'NGCount2'
,MAX(case CheckReason when '2-某批计划开始生产' then OKCount else 0 end) 'OKCount2'
,MAX(case CheckReason when '3-模具维修后恢复生产' then NGCount else 0 end) 'NGCount3'
,MAX(case CheckReason when '3-模具维修后恢复生产' then OKCount else 0 end) 'OKCount3'
,MAX(case CheckReason when '4-设备故障修复后重新生产' then NGCount else 0 end) 'NGCount4'
,MAX(case CheckReason when '4-设备故障修复后重新生产' then OKCount else 0 end) 'OKCount4'
,MAX(case CheckReason when '5-中途参数调整' then NGCount else 0 end) 'NGCount5'
,MAX(case CheckReason when '5-中途参数调整' then OKCount else 0 end) 'OKCount5'
,MAX(case CheckReason when '6-材料厂家、批次更换' then NGCount else 0 end) 'NGCount6'
,MAX(case CheckReason when '6-材料厂家、批次更换' then OKCount else 0 end) 'OKCount6'
,MAX(case CheckReason when '7-设备更换(含辅机)' then NGCount else 0 end) 'NGCount7'
,MAX(case CheckReason when '7-设备更换(含辅机)' then OKCount else 0 end) 'OKCount7'
,MAX(case CheckReason when '8-其它原因临时停产后恢复生产' then NGCount else 0 end) 'NGCount8'
,MAX(case CheckReason when '8-其它原因临时停产后恢复生产' then OKCount else 0 end) 'OKCount8'
from #t_ResultInspect
group by Date,BC,ItemNo) r on c.Date=r.Date and r.BC=c.BC and r.ItemNo=c.dItemNo
order by r.Date,c.DeptCode

/*分页*/
declare @row_from int, @row_to int
select @out_total_rows = count(*) from #t_PageFirstCollect
execute st_MES_RS_Pages @out_total_rows, @in_pagesize, @in_page, @row_from output, @row_to output

if((@in_page=case when @out_total_rows%@in_pagesize>0 then @out_total_rows/@in_pagesize+1 else @out_total_rows/@in_pagesize end) or @in_pagesize=-1)
begin
select seq,CustNo, MonthName,Station,ItemDesc,DeptName,Date,rate,NGItemList
,InspectCount,NGCount,OKCount,AbnormalNO
,NGCount1,NGCount2,NGCount3,NGCount4,NGCount5,NGCount6,NGCount7,NGCount8
,OKCount1,OKCount2,OKCount3,OKCount4,OKCount5,OKCount6,OKCount7,OKCount8

from #t_PageFirstCollect where seq between @row_from and @row_to
union
select seq=max(seq)+1,'',MonthName='','','','','总合计',CONVERT(varchar(6),CONVERT(decimal(18,2),sum(OKCount)/convert(float,sum(InspectCount))*100.00))+'%','',SUM(InspectCount),SUM(NGCount),SUM(OKCount),''
,sum(NGCount1),sum(NGCount2),sum(NGCount3),sum(NGCount4),sum(NGCount5),sum(NGCount6),sum(NGCount7),sum(NGCount8)
,SUM(OKCount1),SUM(OKCount2),SUM(OKCount3),SUM(OKCount4),SUM(OKCount5),SUM(OKCount6),SUM(OKCount7),SUM(OKCount8)
from #t_PageFirstCollect
order by seq
end
else
begin
select seq,CustNo, MonthName,Station,ItemDesc,DeptName,Date,rate,NGItemList
,InspectCount,NGCount,OKCount,AbnormalNO
,NGCount1,NGCount2,NGCount3,NGCount4,NGCount5,NGCount6,NGCount7,NGCount8
,OKCount1,OKCount2,OKCount3,OKCount4,OKCount5,OKCount6,OKCount7,OKCount8
from #t_PageFirstCollect where seq between @row_from and @row_to
order by seq
end

print @out_total_rows
select TotalCount=@out_total_rows
drop table #t_PageFirstCollect
drop table #t_ResultInspect
drop table #t_BCInspect
--drop table #t_BillNoList
END

上一篇:Winform 进程、线程、treeview


下一篇:计蒜客模拟赛D1T2 蒜头君的树:树上节点之间最短距离和