11,849
社区成员
发帖
与我相关
我的任务
分享
ALTER proc [Material].[GetIORecordAndStrock]
@InventoryID AS INT
as
select
a.RecordCode
,cast(cast(a.RecordDate as DATE) as varchar)RecordDate
,c.Name
,case c.IsOut when 1 then b.Quantity else 0 end OutQty
,case c.Isout When 1 then 0 else b.Quantity end InQty
,case c.IsOut when 1 then b.Quantity *-1 else b.Quantity end cQty
,cast(DATEPART(m,a.recordDate)as varchar(2)) dMonth
,ROW_NUMBER ()over(order by a.recorddate,a.recordid) sn
into #t
from
Material.Records a join Material.RecordDetail b on a.RecordID=b.RecordID
join Material.IOCategory c on a.CategoryID =c.CategoryID
where b.InventoryID =@InventoryID
union all
select ''单据号,
''单据日期,
'期初结存' 摘要,
0 出库数量 ,
0 入库数量,
OpeningStock 结存数量,
0 月份 ,
0
From Material.Inventory where InventoryID=@InventoryID ;
--上面的代码效率有问题
--select * from #t
create table #t1
(
id [int] identity(1,1) not null
,单据号 varchar(50) null
,单据日期 varchar(50) null
,摘要 varchar(50) null
,出库数量 decimal(18,4) null
,入库数量 decimal(18,4) null
,结存数量 decimal(18,4) null
,月份 int
CONSTRAINT [PK_AlignStyle] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
insert #t1(
单据号
,单据日期
,摘要
,出库数量
,入库数量
,结存数量
,月份)
select
''单据号
,''单据日期
,'期初结存' 摘要
,0 出库数量
,0 入库数量
,cQty 结存数量
,cast( 0 as varchar(2)) 月份
From #t where sn=0
declare @month as int=1
while @month <13
begin
insert #t1(
单据号
,单据日期
,摘要
,出库数量
,入库数量
,结存数量
,月份)
select
RecordCode
,RecordDate
,Name
,Nullif(OutQty,0)
,nullif(InQty ,0)
,(select sum(cQty) from #t where sn<=a.sn)
,dMonth
from #t a where dMonth =@month
union all
select
''
,''
,'本月合计'
,SUM(OutQty)
,SUM(InQty)
,(select SUM(cQTY) from #t where dMonth <=@month )
,@month
from #t
where dMonth =@month and exists(select 1 from #t where dMonth =@month )
union all
select
''
,''
,'本年累计'
,SUM(OutQty)
,SUM(InQty)
,SUM(cQTY)
,@month from #t
where dMonth <=@month and exists(select 1 from #t where dMonth =@month )
set @month +=1
end
select * from #t1 where coalesce(出库数量,入库数量) is not null order by id