金蝶K3库存分类汇总表

按照仓库单据的类型分类统计出日期段内各种物料各种类型的单据出入数量

create Proc stkd_库存分类汇总表     
 
@BegDate DateTime,@EndDate DateTime,
 
@BegItem Varchar(100),@EndItem Varchar(100),
@stock varchar(20)
As

--Select @BegDate='2004-01-01',@EndDate='2004-10-29',@BegItem='',@EndItem='',@BegCust='',@EndCust=''
select @enditem='zzzz' where @enditem=''

Set NoCount On

declare @st int
select @st=fitemid from t_stock where fname=@stock

create table #aa(
fitemid 
int,
fnumber 
varchar(80),
fname 
varchar(80),
fmodel 
varchar(50),
fbeg 
decimal(18,2default(0),
fpur 
decimal(18,2default(0),
fbz 
decimal(18,2default(0),
fling 
decimal(18,2default(0),
fdin 
decimal(18,2default(0),
fdout 
decimal(18,2default(0),
fpin 
decimal(18,2default(0),
fpout 
decimal(18,2default(0),
fsale 
decimal(18,2default(0),
fqin 
decimal(18,2default(0),
fqout 
decimal(18,2default(0),
fend 
decimal(18,2default(0)
)

declare @beg varchar(10
select @beg=cast(year(@begdateas varchar(4))+'-'+cast(month(@begdateas varchar(2))
+'-01'

insert into #aa(fitemid,fnumber,fname,fmodel)
select fitemid,fnumber,fname,fmodel 
from t_icitem
where fnumber>=@begitem and fnumber<=@enditem

update t1
set t1.fbeg=t2.fbegbal
from #aa t1,
(
select fitemid,sum(fbegqty) as fbegbal from icinvbal
where fstockid in (select fitemid from t_stock where fname like '%'+@stock+'%')
and fyear=year(@begdateand fperiod=month(@begdate)
group by fitemid)
t2
where t1.fitemid=t2.fitemid

update t1
set t1.fbeg=t1.fbeg+t2.fbal
from #aa t1,(
select ice.fitemid,
sum(case when ic.ftrantype in (1,2,3,5,10,40,41and ice.fdcstockid=@st then ice.fqty 
    
when ic.ftrantype in (21,24,29,28,43,41and ice.fscstockid=@st then -ice.fqty else 0 endas fbal
from icstockbill ic,icstockbillentry ice
where ic.finterid=ice.finterid and ic.fdate>=@beg and ic.fdate<@begdate
group by ice.fitemid
) t2
where t1.fitemid=t2.fitemid



update t1
set t1.fpur=t2.fpur,t1.fbz=t2.fbz,t1.fqin=t2.fqin,
t1.fqout
=t2.fqout,t1.fsale=t2.fsale,t1.fpin=t2.fpin,t1.fpout=t2.fpout,t1.fling=t2.fling,
t1.fdin
=t2.fdin,t1.fdout=t2.fdout
from #aa t1,(
select fitemid,
sum(case when ic.ftrantype=1 and ice.fdcstockid=@st then ice.fqty else 0 endas fpur,
sum(case when ic.ftrantype=2 and ice.fdcstockid=@st then ice.fqty else 0 endas fbz,
sum(case when ic.ftrantype=10 and ice.fdcstockid=@st then ice.fqty else 0 endas fqin,
sum(case when ic.ftrantype=21 and ice.fdcstockid=@st then ice.fqty else 0 endas fsale,
sum(case when ic.ftrantype=24 and ice.fscstockid=@st then ice.fqty else 0 endas fling,
sum(case when ic.ftrantype=29 and ice.fdcstockid=@st then ice.fqty else 0 endas fqout,
sum(case when ic.ftrantype=40 and ice.fdcstockid=@st then ice.fqty else 0 endas fpin,
sum(case when ic.ftrantype=43 and ice.fdcstockid=@st then ice.fqty else 0 endas fpout,
sum(case when ic.ftrantype=41 and ice.fdcstockid=@st then ice.fqty else 0 endas fdin,
sum(case when ic.ftrantype=41 and ice.fscstockid=@st then ice.fqty else 0 endas fdout
from icstockbill ic,icstockbillentry ice
where ic.finterid=ice.finterid and ic.fdate>=@begdate and ic.fdate<=@enddate
group by ice.fitemid
)
t2 
where t1.fitemid=t2.fitemid



update #aa set fend=fbeg+fpur+fbz+fqin+fdin+fpin-fdout-fqout-fling-fpout-fsale

delete from #aa where fbeg=0 and fend=0 and fqout=0 
and fqin=0 and fpur=0 and fsale=0

select fnumber as 物料编码,fname as 物料名称,fmodel as 规格,fbeg as 期初,fpur as 外购,
fbz 
as 产品入库,fdin as 调拨入库,
fdout 
as 调拨出库,fpin as 盘盈入库,fpout as 盘盈出库,fling as 领料,fsale as 销售出库,
fqin 
as 其他入库,fqout as 其他出库,fend as 期末
from #aa order by fnumber



GO
posted @   arrowolf  阅读(1971)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示