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

Mes首检确认统计的存储过程的更多相关文章

  1. H5性能测试,首屏时间统计(Argus)

    Argus 腾讯质量开发平台,官网链接:https://wetest.qq.com/product/argus 主要针对性:H5的游戏性能测试 主要介绍: 独家首屏时间统计: 告别人工掐秒 自动统计首 ...

  2. MES系统的有用存储过程

    USE [ChiefmesNEW]GO/****** Object: StoredProcedure [dbo].[st_WMS_ImportStockInBill] Script Date: 10/ ...

  3. 汽车制造商表态:必须依靠MES系统来管控流程

    汽车行业特点 汽车工业是一个高投入,高产出,集群式发展的产业部门. 汽车自身的投资,生产,研发,供应,销售,维修:前序的原材料,零部件,技术装备,物流:后序的油料,服务,信贷,咨询,保险,直至基础设施 ...

  4. JDBC之存储过程

    存储过程的语法创建就不说了,这里这篇博客 就挺详细了http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html. 1. Java代码调用没有参数的存错过程 ...

  5. iBatis调用存储过程以及MySQL创建存储过程

    首先是MySQL中创建存储过程的SQL -- 列出全部的存储过程 SHOW PROCEDURE STATUS; -- 查看一个已存在的存储过程的创建语句,假设此存储过程不存在,会提示SQL错误(130 ...

  6. 14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB

    14.6.11 Configuring Optimizer Statistics for InnoDB 配置优化统计信息用于InnoDB 14.6.11.1 Configuring Persisten ...

  7. 白话空间统计之四:P值和Z值(上):零如果

    本来今天想要讲讲软件操作的,后来发现好像还有好几个重要的指标没有说,干脆等所有说完在讲操作吧.否则操作出来的结果会发现大量的"不明觉厉". 首先是空间统计里面非常神奇的两个值:P值 ...

  8. 10-SQLServer中统计信息的使用

    一.总结 1.网址https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-stats-tr ...

  9. MySQL统计库表大小

    统计每个库每个表的大小是数据治理的其中最简单的一个要求,本文将从抽样统计结果及精确统计结果两方面来统计MySQL的每个库每个表的数据量情况. 1.统计预估数据量 mysql数据字典库informati ...

随机推荐

  1. 递归神经网络(Recurrent Neural Networks,RNN)

    在深度学习领域,传统的多层感知机(MLP)具有出色的表现,取得了许多成功,它曾在许多不同的任务上——包括手写数字识别和目标分类上创造了记录.甚至到了今天,MLP在解决分类任务上始终都比其他方法要略胜一 ...

  2. session_start保存的客户端cookie的值什么时候改变

    //cookie记录的session_id立刻改变了session_start();echo "old:".session_id();session_regenerate_id() ...

  3. php的session_start

    如果session使用cookie记录,那么在session_start时会设置一个cookie,参数取决于php.ini的设置,当然也可以通过session_set_param在程序里设置.不同站点 ...

  4. 聊聊Dataguard的三种保护模式实验(下)

    4.最大保护模式Maximum Protection 最大保护模式是DG可以提供的最高保护级别,建立在日志同步传输和确认的基础上.同样,可以使用alter database方法进行设置. SQL> ...

  5. PHP Header 缓存 --- Header 参数说明

    1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Charset:   浏览器申 ...

  6. 嵌入式 hi3518平台以太网网络模块设计包括重连机制和网线检测机制

    <span style="font-family:Courier New;"> #include <sys/types.h> #include <st ...

  7. 数组排序,字符串math,replace练习

    1. 关于数组排序     排序一直是程序设计中的热门话题.在js中,数组有个sort()方法.在不传参数默认情况下,数组的sort()方法原理是:对数每项调用toString()方法,比较字符串按升 ...

  8. [转] VS 整合NUnit进行单元测试

    Jeff Wong原文 5分钟实现VS2010整合NUnit进行单元测试 1.下载安装NUnit(最新win版本为NUnit-2.6.0.12051.msi) http://www.nunit.org ...

  9. yii 操作session和cookie

    一,在Yii中使用session 1,CHttpSession 与原生态php5的session使用差别是,php5使用session_start();$_session['key'] = $valu ...

  10. Textbox像百度一下实现下拉显示 z

    List<string> Data = new List<string>(); string Randomstr = "功夫撒黑胡椒hcbvf蜂窝qwertyuiop ...