求存储过程!!!

meilian02 2004-08-11 04:44:36
表A

PartNo LotQty SampleQty BadQty Date CheckTimes --第几次检查
A 50 30 0 2004.07.10 1 --这个不属于二十九周
B 20 10 5 2004.07.11 1
A 60 20 0 2004.07.14 1
C 90 30 10 2004.07.15 2
B 30 10 0 2004.07.16 3
C 40 10 0 2004.07.12 1
A 40 30 0 2004.07.12 1

想得到结果
--输入时间2004.7.14 (2004年第二十九周)

首次抽检 返工抽检
产品编号 抽检批数 接收批数 LAR% 抽检批数 接收批数 LAR% 样本总数量 抽检总批数 接收总批数 DPPM
抽检产品数 接收产品数 抽检产品数 接收产品数 样本不良品数 抽检总数 接收总产品数

A 2 2 100% 0 0 0 50 2 2 0
100 100 0 0 0 100 100

B 1 0 0 1 1 100% 20 2 1 0.25
20 0 30 30 5 50 30

C 1 1 100% 1 0 0 40 2 1 0.25
40 40 90 0 10 130 40


注:LAR = 接收批数/抽检批数
DPPM = 样本不良品数/样本总数量
...全文
230 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
meilian02 2004-08-12
  • 打赏
  • 举报
回复
谢谢谢谢!!

邹建大哥,邹大侠!
meilian02 2004-08-12
  • 打赏
  • 举报
回复
看到你的结果及你的速度!

我唯一只有目瞪口呆!

zjcxc 元老 2004-08-12
  • 打赏
  • 举报
回复
--测试数据
create table A(PartNo varchar(10),LotNo varchar(10),LotQty int,SampleQty int,BadQty int,Date datetime,CheckTimes int)
insert A select 'A','2004A',50,30,0 ,'2004.07.10',1
union all select 'B','2004B',20,10,5 ,'2004.07.11',1
union all select 'A','2004A',60,20,0 ,'2004.07.14',1
union all select 'C','2004B',90,30,10,'2004.07.15',2
union all select 'B','2004C',30,10,0 ,'2004.07.16',3
union all select 'C','2004D',40,10,0 ,'2004.07.12',1
union all select 'A','2004F',40,30,0 ,'2004.07.12',1
go

--查询的存储过程
create proc p_qry
@dt datetime,
@where varchar(8000) --加了一个条件,此条件从程序中传过来
as
set @where='datediff(week,Date,'''+cast(@dt as varchar)+''')=0 '
+case isnull(@where,'') when '' then ''
else ' and ('+@where+')' end

exec('
select 产品编号=case s2 when 1 then 产品编号 else '''' end
,首次抽检_抽检批数,首次抽检_抽检批数,[首次抽检_LAR%]
,返工抽检_抽检批数,返工抽检_接收批数,[返工抽检_LAR%]
,样本总数量,抽检总批数,接收总批数,[DPPM]=[LAR%]
from(
select 产品编号=PartNo
,首次抽检_抽检批数=sum(case when CheckTimes=0 then 1 else 0 end)
,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then 1 else 0 end)
,[首次抽检_LAR%]=case sum(case when CheckTimes=0 then 1 else 0 end)
when 0 then ''0%''
else cast(cast(
sum(case when CheckTimes=0 and BadQty=0 then 100.0 else 0.0 end)
/sum(case when CheckTimes=0 then 1 else 0 end)
as decimal(10,2)) as varchar)+''%'' end
,返工抽检_抽检批数=sum(case when CheckTimes>0 then 1 else 0 end)
,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then 1 else 0 end)
,[返工抽检_LAR%]=case sum(case when CheckTimes>0 then 1 else 0 end)
when 0 then ''0%''
else cast(cast(
sum(case when CheckTimes>0 and BadQty=0 then 100.0 else 0.0 end)
/sum(case when CheckTimes>0 then 1 else 0 end)
as decimal(10,2)) as varchar)+''%'' end
,样本总数量=Sum(SampleQty)
,抽检总批数=sum(1)
,接收总批数=sum(case when BadQty=0 then 1 else 0 end)
,[LAR%]=case sum(1)
when 0 then ''0%''
else cast(cast(
sum(case when BadQty=0 then 100.0 else 0.0 end)
/sum(1)
as decimal(10,2)) as varchar)+''%'' end
,s1=PartNo,s2=1
from A
where '+@where+'
group by PartNo

union all

select 产品编号=PartNo
,首次抽检_抽检批数=sum(case when CheckTimes=0 then LotQty else 0 end)
,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then LotQty else 0 end)
,[首次抽检_LAR%]=''''
,返工抽检_抽检批数=sum(case when CheckTimes>0 then LotQty else 0 end)
,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then LotQty else 0 end)
,[返工抽检_LAR%]=''''
,样本不良品数=Sum(BadQty)
,抽检总数=sum(LotQty)
,接收总产品数=sum(case when BadQty=0 then LotQty else 0 end)
,[DPPM]=case Sum(SampleQty)
when 0 then ''0%''
else cast(cast(
Sum(BadQty)*100.9
/Sum(SampleQty)
as decimal(10,2)) as varchar)+''%'' end
,s1=PartNo,s2=2
from A
where '+@where+'
group by PartNo
)a order by s1,s2
')
go

--调用
exec p_qry '2004.7.14', 'PartNo=''A'''
go

--删除测试
drop table A
drop proc p_qry

--结果自己看
meilian02 2004-08-12
  • 打赏
  • 举报
回复
为什么看不到?
再踩一脚!
meilian02 2004-08-12
  • 打赏
  • 举报
回复
--测试数据
create table A(PartNo varchar(10),LotNo varchar(10),LotQty int,SampleQty int,BadQty int,Date datetime,CheckTimes int)
insert A select 'A','2004A',50,30,0 ,'2004.07.10',1
union all select 'B','2004B',20,10,5 ,'2004.07.11',1
union all select 'A','2004A',60,20,0 ,'2004.07.14',1
union all select 'C','2004B',90,30,10,'2004.07.15',2
union all select 'B','2004C',30,10,0 ,'2004.07.16',3
union all select 'C','2004D',40,10,0 ,'2004.07.12',1
union all select 'A','2004F',40,30,0 ,'2004.07.12',1
go

--查询的存储过程
create proc p_qry
@dt datetime, @where varchar(8000) --加了一个条件,此条件从程序中传过来
as
declare @s varchar(8000) --增加了一个变量,将所有的语句全存进去,因为条件不能直接接在 where 后面,我只能想到这个笨办法

set @s = N'select 产品编号=case s2 when 1 then 产品编号 else '''' end
,首次抽检_抽检批数,首次抽检_抽检批数,[首次抽检_LAR%]
,返工抽检_抽检批数,返工抽检_接收批数,[返工抽检_LAR%]
,样本总数量,抽检总批数,接收总批数,[DPPM]=[LAR%]
from(
select 产品编号=PartNo
,首次抽检_抽检批数=sum(case when CheckTimes=0 then 1 else 0 end)
,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then 1 else 0 end)
,[首次抽检_LAR%]=case sum(case when CheckTimes=0 then 1 else 0 end)
when 0 then ''0%''
else cast(cast(
sum(case when CheckTimes=0 and BadQty=0 then 100.0 else 0.0 end)
/sum(case when CheckTimes=0 then 1 else 0 end)
as decimal(10,2)) as varchar)+''%'' end
,返工抽检_抽检批数=sum(case when CheckTimes>0 then 1 else 0 end)
,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then 1 else 0 end)
,[返工抽检_LAR%]=case sum(case when CheckTimes>0 then 1 else 0 end)
when 0 then ''0%''
else cast(cast(
sum(case when CheckTimes>0 and BadQty=0 then 100.0 else 0.0 end)
/sum(case when CheckTimes>0 then 1 else 0 end)
as decimal(10,2)) as varchar)+''%'' end
,样本总数量=Sum(SampleQty)
,抽检总批数=sum(1)
,接收总批数=sum(case when BadQty=0 then 1 else 0 end)
,[LAR%]=case sum(1)
when 0 then ''0%''
else cast(cast(
sum(case when BadQty=0 then 100.0 else 0.0 end)
/sum(1)
as decimal(10,2)) as varchar)+''%'' end
,s1=PartNo,s2=1
from A
where datediff(week,Date,@dt)=0' + @where -- 这个变量新加的

set @s = @s+N' group by PartNo

union all

select 产品编号=PartNo
,首次抽检_抽检批数=sum(case when CheckTimes=0 then LotQty else 0 end)
,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then LotQty else 0 end)
,[首次抽检_LAR%]=''''
,返工抽检_抽检批数=sum(case when CheckTimes>0 then LotQty else 0 end)
,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then LotQty else 0 end)
,[返工抽检_LAR%]=''''
,样本不良品数=Sum(BadQty)
,抽检总数=sum(LotQty)
,接收总产品数=sum(case when BadQty=0 then LotQty else 0 end)
,[DPPM]=case Sum(SampleQty)
when 0 then ''0%''
else cast(cast(
Sum(BadQty)*100.9
/Sum(SampleQty)
as decimal(10,2)) as varchar)+''%'' end
,s1=PartNo,s2=2
from A
where datediff(week,Date,@dt)=0' + @where -- 这个也是

set @s = @s+N' group by PartNo
)a order by s1,s2'

exec(@s)
go

--调用
exec p_qry '2004.7.14', ''
go

--删除测试
drop table A
drop proc p_qry

-- 运行后出现如下错误

/*(所影响的行数为 7 行)

服务器: 消息 170,级别 15,状态 1,行 1
第 1 行: '?' 附近有语法错误。

*/

--大家有更好的方法吗?
zjcxc 元老 2004-08-11
  • 打赏
  • 举报
回复
--测试

--测试数据
create table A(PartNo varchar(10),LotNo varchar(10),LotQty int,SampleQty int,BadQty int,Date datetime,CheckTimes int)
insert A select 'A','2004A',50,30,0 ,'2004.07.10',1
union all select 'B','2004B',20,10,5 ,'2004.07.11',1
union all select 'A','2004A',60,20,0 ,'2004.07.14',1
union all select 'C','2004B',90,30,10,'2004.07.15',2
union all select 'B','2004C',30,10,0 ,'2004.07.16',3
union all select 'C','2004D',40,10,0 ,'2004.07.12',1
union all select 'A','2004F',40,30,0 ,'2004.07.12',1
go

--查询的存储过程
create proc p_qry
@dt datetime --要查询的日期
as
select 产品编号=case s2 when 1 then 产品编号 else '' end
,首次抽检_抽检批数,首次抽检_抽检批数,[首次抽检_LAR%]
,返工抽检_抽检批数,返工抽检_接收批数,[返工抽检_LAR%]
,样本总数量,抽检总批数,接收总批数,[DPPM]=[LAR%]
from(
select 产品编号=PartNo
,首次抽检_抽检批数=sum(case when CheckTimes=0 then 1 else 0 end)
,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then 1 else 0 end)
,[首次抽检_LAR%]=case sum(case when CheckTimes=0 then 1 else 0 end)
when 0 then '0%'
else cast(cast(
sum(case when CheckTimes=0 and BadQty=0 then 100.0 else 0.0 end)
/sum(case when CheckTimes=0 then 1 else 0 end)
as decimal(10,2)) as varchar)+'%' end
,返工抽检_抽检批数=sum(case when CheckTimes>0 then 1 else 0 end)
,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then 1 else 0 end)
,[返工抽检_LAR%]=case sum(case when CheckTimes>0 then 1 else 0 end)
when 0 then '0%'
else cast(cast(
sum(case when CheckTimes>0 and BadQty=0 then 100.0 else 0.0 end)
/sum(case when CheckTimes>0 then 1 else 0 end)
as decimal(10,2)) as varchar)+'%' end
,样本总数量=Sum(SampleQty)
,抽检总批数=sum(1)
,接收总批数=sum(case when BadQty=0 then 1 else 0 end)
,[LAR%]=case sum(1)
when 0 then '0%'
else cast(cast(
sum(case when BadQty=0 then 100.0 else 0.0 end)
/sum(1)
as decimal(10,2)) as varchar)+'%' end
,s1=PartNo,s2=1
from A
where datediff(week,Date,@dt)=0
group by PartNo

union all

select 产品编号=PartNo
,首次抽检_抽检批数=sum(case when CheckTimes=0 then LotQty else 0 end)
,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then LotQty else 0 end)
,[首次抽检_LAR%]=''
,返工抽检_抽检批数=sum(case when CheckTimes>0 then LotQty else 0 end)
,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then LotQty else 0 end)
,[返工抽检_LAR%]=''
,样本不良品数=Sum(BadQty)
,抽检总数=sum(LotQty)
,接收总产品数=sum(case when BadQty=0 then LotQty else 0 end)
,[DPPM]=case Sum(SampleQty)
when 0 then '0%'
else cast(cast(
Sum(BadQty)*100.9
/Sum(SampleQty)
as decimal(10,2)) as varchar)+'%' end
,s1=PartNo,s2=2
from A
where datediff(week,Date,@dt)=0
group by PartNo
)a order by s1,s2
go

--调用
exec p_qry '2004.7.14'
go

--删除测试
drop table A
drop proc p_qry

/*--测试结果(自己看)
--*/
zjcxc 元老 2004-08-11
  • 打赏
  • 举报
回复
--测试

--测试数据
create table A(PartNo varchar(10),LotNo varchar(10),LotQty int,SampleQty int,BadQty int,Date datetime,CheckTimes int)
insert A select 'A','2004A',50,30,0 ,'2004.07.10',1
union all select 'B','2004B',20,10,5 ,'2004.07.11',1
union all select 'A','2004A',60,20,0 ,'2004.07.14',1
union all select 'C','2004B',90,30,10,'2004.07.15',2
union all select 'B','2004C',30,10,0 ,'2004.07.16',3
union all select 'C','2004D',40,10,0 ,'2004.07.12',1
union all select 'A','2004F',40,30,0 ,'2004.07.12',1
go

--查询
select 产品编号=case s2 when 1 then 产品编号 else '' end
,首次抽检_抽检批数,首次抽检_抽检批数,[首次抽检_LAR%]
,返工抽检_抽检批数,返工抽检_接收批数,[返工抽检_LAR%]
,样本总数量,抽检总批数,接收总批数,[DPPM]=[LAR%]
from(
select 产品编号=PartNo
,首次抽检_抽检批数=sum(case when CheckTimes=0 then 1 else 0 end)
,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then 1 else 0 end)
,[首次抽检_LAR%]=case sum(case when CheckTimes=0 then 1 else 0 end)
when 0 then '0%'
else cast(cast(
sum(case when CheckTimes=0 and BadQty=0 then 100.0 else 0.0 end)
/sum(case when CheckTimes=0 then 1 else 0 end)
as decimal(10,2)) as varchar)+'%' end
,返工抽检_抽检批数=sum(case when CheckTimes>0 then 1 else 0 end)
,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then 1 else 0 end)
,[返工抽检_LAR%]=case sum(case when CheckTimes>0 then 1 else 0 end)
when 0 then '0%'
else cast(cast(
sum(case when CheckTimes>0 and BadQty=0 then 100.0 else 0.0 end)
/sum(case when CheckTimes>0 then 1 else 0 end)
as decimal(10,2)) as varchar)+'%' end
,样本总数量=Sum(SampleQty)
,抽检总批数=sum(1)
,接收总批数=sum(case when BadQty=0 then 1 else 0 end)
,[LAR%]=case sum(1)
when 0 then '0%'
else cast(cast(
sum(case when BadQty=0 then 100.0 else 0.0 end)
/sum(1)
as decimal(10,2)) as varchar)+'%' end
,s1=PartNo,s2=1
from A
where datediff(week,Date,'2004.7.14')=0
group by PartNo

union all

select 产品编号=PartNo
,首次抽检_抽检批数=sum(case when CheckTimes=0 then LotQty else 0 end)
,首次抽检_接收批数=sum(case when CheckTimes=0 and BadQty=0 then LotQty else 0 end)
,[首次抽检_LAR%]=''
,返工抽检_抽检批数=sum(case when CheckTimes>0 then LotQty else 0 end)
,返工抽检_接收批数=sum(case when CheckTimes>0 and BadQty=0 then LotQty else 0 end)
,[返工抽检_LAR%]=''
,样本不良品数=Sum(BadQty)
,抽检总数=sum(LotQty)
,接收总产品数=sum(case when BadQty=0 then LotQty else 0 end)
,[DPPM]=case Sum(SampleQty)
when 0 then '0%'
else cast(cast(
Sum(BadQty)*100.9
/Sum(SampleQty)
as decimal(10,2)) as varchar)+'%' end
,s1=PartNo,s2=2
from A
where datediff(week,Date,'2004.7.14')=0
group by PartNo
)a order by s1,s2
go

--删除测试
drop table A

/*--测试结果(自己看)
--*/
meilian02 2004-08-11
  • 打赏
  • 举报
回复
不是显示格式的问题!

当然我要的就是下面数据的结果!

最难做的是那个DPPM,它就是要拿下一行的 样本不良品数/样本总数量
不知道这样的结果在一个表里能否实现!

pbsql 2004-08-11
  • 打赏
  • 举报
回复
不要把显示格式放到这里来做
meilian02 2004-08-11
  • 打赏
  • 举报
回复
表A

PartNo LotNo LotQty SampleQty BadQty Date CheckTimes --第几次检查
A 2004A 50 30 0 2004.07.10 1 --这个不属于二十九周
B 2004B 20 10 5 2004.07.11 1
A 2004A 60 20 0 2004.07.14 1
C 2004B 90 30 10 2004.07.15 2
B 2004C 30 10 0 2004.07.16 3
C 2004D 40 10 0 2004.07.12 1
A 2004F 40 30 0 2004.07.12 1

想得到结果
--输入时间2004.7.14 (2004年第二十九周)

首次抽检 返工抽检
产品编号 抽检批数 接收批数 LAR% 抽检批数 接收批数 LAR% 样本总数量 抽检总批数 接收总批数 DPPM
抽检产品数 接收产品数 抽检产品数 接收产品数 样本不良品数 抽检总数 接收总产品数

A 2 2 100% 0 0 0 50 2 2 0
100 100 0 0 0 100 100

B 1 0 0 1 1 100% 20 2 1 0.25
20 0 30 30 5 50 30

C 1 1 100% 1 0 0 40 2 1 0.25
40 40 90 0 10 130 40



每一种厂品占两行

第一行:抽检批数等
第二行:抽检产品数等

以下是各数据的计算方法

抽检批数 = Count(LotNo)
接收批数 = Count(LotNo) where BadQty = 0

样本总数量 = Sum(SampleQty)
样本不良品数 = Sum(BadQty)

CheckTimes = 0 是首次检查
CheckTimes > 0 是返工抽检

LAR = 接收批数/抽检批数
DPPM = 样本不良品数/样本总数量



不知道这样够不够清楚!
meilian02 2004-08-11
  • 打赏
  • 举报
回复
不是吧!大家都看不明白?老大也看不明白?

等等,我再理理!

:(
52juanjuan 2004-08-11
  • 打赏
  • 举报
回复
看不懂了.
zjcxc 元老 2004-08-11
  • 打赏
  • 举报
回复
看不明白
meilian02 2004-08-11
  • 打赏
  • 举报
回复
我是整理过了的!到写字板里不会乱的呀!
这里宽度不够,再怎么调也调不好!

大家帮帮忙!

谢啦!

^_^
WangZWang 2004-08-11
  • 打赏
  • 举报
回复
看不出如何生成的结果?
pbsql 2004-08-11
  • 打赏
  • 举报
回复
你还是整理一下吧,看着太累
sankis 2004-08-11
  • 打赏
  • 举报
回复
好乱,到写字板里也乱。
meilian02 2004-08-11
  • 打赏
  • 举报
回复
大家COPY到写字板里面看好吗?在这里怎么都排不好!

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧