declare @m int=1,@n int,@s varchar(20),@a varchar(20),@t datetime='20210101'
--查询是否有匹配不上的发货料品
set @a=convert(VARCHAR(6),@t,112)
SELECT @n=count(*) from u9_month_fhsl a
left join u9_month_rkcb b on a.rq=b.kjqj and a.itemcode=b.code
WHERE (b.matercost is null and b.prodcost is null and b.machcost is null and b.labcost is null)
and a.rq=CONVERT (VARCHAR (6),@t,112)
--若有,进行循环处理
if(@n>0)
BEGIN
set @s=convert(VARCHAR(6),@t,112)
create table #H1(rq varchar(50),Area varchar(20),Payer_Code varchar(50),shortname varchar(100),Category varchar(20),itemcode varchar(100),ItemName varchar(100),StockNum varchar(100),kcnum float,weightKg float,TMoneyTC float,TMoneyFC float,材料 float,人工 float,制造 float,机器辐照 float,水电汽 float,mlrate float)
insert into #H1 exec u9_month_glbs @rq=@s
while (@m<=6)
BEGIN
set @s=convert(VARCHAR(6),DATEADD(month,-@m,@t),112)
update #H1 set 材料=m1,人工=m2,制造=m3,机器辐照=m4,水电汽=m5,mlrate=m6
FROM (SELECT b.code,(#H1.weightKg/b.CostKg)*b.matercost m1,(#H1.weightKg/b.CostKg)*b.labcost m2,(#H1.weightKg/b.CostKg)*b.prodcost m3,(#H1.weightKg/b.CostKg)*b.machcost m4,
(#H1.weightKg/b.CostKg)*b.contrcost m5,100*(#H1.TMoneyFC-(#H1.weightKg/b.CostKg)*b.matercost-(#H1.weightKg/b.CostKg)*b.labcost-(#H1.weightKg/b.CostKg)*b.prodcost-(#H1.weightKg/b.CostKg)*b.machcost)/#H1.TMoneyFC m6 FROM #H1
left join u9_month_rkcb b on b.kjqj=@s and #H1.itemcode=b.code
WHERE #H1.材料 is null and #H1.人工 is null and #H1.制造 is null and #H1.机器辐照 is null and #H1.水电汽 is null and #H1.itemcode=b.code) c
where #H1.itemcode=c.code
set @m=@m+1
END
END
SELECT rq,Area,Payer_Code,shortname,Category,itemcode,ItemName,StockNum,kcnum,Convert(decimal(18,2),weightKg) weightKg,Convert(decimal(18,2),TMoneyTC) TMoneyTC,Convert(decimal(18,2),TMoneyFC) TMoneyFC,Convert(decimal(18,2),isnull(材料,0)) 材料,Convert(decimal(18,2),isnull(人工,0)) 人工,Convert(decimal(18,2),isnull(制造,0)) 制造,Convert(decimal(18,2),isnull(机器辐照,0)) 机器辐照,Convert(decimal(18,2),isnull(水电汽,0)) 水电汽,Convert(decimal(18,2),isnull(mlrate,100)) mlrate
FROM #H1
union all
select p.rq,p.Area,Payer_Code,p.shortname,p.Category,p.itemcode,p.ItemName,p.StockNum,p.kcnum,Convert(decimal(18,2),p.weightKg) weightKg,Convert(decimal(18,2),p.TMoneyTC) TMoneyTC,Convert(decimal(18,2),p.TMoneyFC) TMoneyFC,Convert(decimal(18,2),isnull(材料,0)) 材料,Convert(decimal(18,2),isnull(人工,0)) 人工,Convert(decimal(18,2),isnull(制造,0)) 制造,Convert(decimal(18,2),isnull(机器辐照,0)) 机器辐照,Convert(decimal(18,2),isnull(水电汽,0)) 水电汽,Convert(decimal(18,2),isnull(mlrate,100)) mlrate
FROM
(select a.rq,cb.name Area,a.Payer_Code,a.shortname,cb3.Name Category,a.itemcode,a.ItemName,a.StockNum,a.kcnum,a.weightKg,a.TMoneyTC,a.TMoneyFC,(a.weightKg/b.CostKg)*b.matercost 材料,(a.weightKg/b.CostKg)*b.labcost 人工,(a.weightKg/b.CostKg)*b.prodcost 制造,(a.weightKg/b.CostKg)*b.machcost 机器辐照,(a.weightKg/b.CostKg)*b.contrcost 水电汽,100*(a.TMoneyFC-(a.weightKg/b.CostKg)*b.matercost-(a.weightKg/b.CostKg)*b.labcost-(a.weightKg/b.CostKg)*b.prodcost-(a.weightKg/b.CostKg)*b.machcost-(a.weightKg/b.CostKg)*b.contrcost)/a.TMoneyFC mlrate
from u9_month_fhsl a
left join u9_month_rkcb b on a.rq=b.kjqj and a.itemcode=b.code
left join (select customercategory,code,shortname FROM CBO_Customer WHERE org in('1001710090001857')) cb1 on a.Payer_Code=cb1.code
left join CBO_CustomerCategory_Trl cb on cb.SysMLFlag='zh-cn' and cb1.CustomerCategory=cb.id
left join (select DISTINCT MainItemCategory,Code from CBO_ItemMaster WHERE org in('1001710090001857','1001710250003355')) cb2 on a.itemcode=cb2.code
left join CBO_Category_Trl cb3 on cb3.SysMLFlag='zh-cn' and cb2.MainItemCategory=cb3.ID
WHERE (b.matercost is not null or b.prodcost is not null or b.machcost is not null or b.labcost is not null or b.contrcost is not null)
and a.rq=@a) p 这是完整的sql,里面调用了那个存储过程,这个sql一开始声明了时间变量,要传这个参数 |