22,209
社区成员
发帖
与我相关
我的任务
分享
select i.结算单号,g.货品名称,isnull(d.期初库存,ld.结余库存) as 期初库存,isnull(d.入库数,0) as 入库数,ISNULL(d.出库数 ,0) as 出库数,isnull(d.结余库存,ld.结余库存) as 结余库存
from (select distinct 结算单号 from #inv ) as i
inner join (select distinct 货品名称 from #inv ) as g on 1=1
left join #inv as d on g.货品名称=d.货品名称 and i.结算单号=d.结算单号
left join #inv as ld on g.货品名称=ld.货品名称 and i.结算单号=ld.结算单号+1
order by i.结算单号,g.货品名称
+--------+------+------+-----+-----+------+
| 结算单号 | 货品名称 | 期初库存 | 入库数 | 出库数 | 结余库存 |
+--------+------+------+-----+-----+------+
| 100011 | A | 0 | 200 | 100 | 100 |
| 100011 | B | 0 | 50 | 20 | 30 |
| 100012 | A | 100 | 0 | 10 | 90 |
| 100012 | B | 30 | 0 | 0 | 30 |
| 100013 | A | 90 | 0 | 0 | 90 |
| 100013 | B | 30 | 0 | 5 | 25 |
+--------+------+------+-----+-----+------+
-- 已有表生成机制是这样
create table #inv
(序号 int,结算单号 varchar(10),货品名称 varchar(10),期初库存 int,入库数 int,出库数 int,结余库存 int)
insert into #inv
select 1,'100011','A',0,200,100,100 union all
select 2,'100011','B',0,50,20,30 union all
select 3,'100012','A',100,0,10,90 union all
select 4,'100013','B',30,0,5,25
-- 补齐数据
insert into #inv(结算单号,货品名称,期初库存,入库数,出库数,结余库存)
select 结算单号=a.结算单号,
货品名称=b.货品名称,
期初库存=isnull((select top 1 e.结余库存
from #inv e
where e.货品名称=b.货品名称
and e.结算单号<a.结算单号
order by e.结算单号 desc),0),
入库数=0,
出库数=0,
结余库存=isnull((select top 1 e.结余库存
from #inv e
where e.货品名称=b.货品名称
and e.结算单号<a.结算单号
order by e.结算单号 desc),0)
from (select distinct 结算单号 from #inv) a
cross join (select distinct 货品名称 from #inv) b
left join #inv c on a.结算单号=c.结算单号
and b.货品名称=c.货品名称
where c.结算单号 is null
select 序号=identity(int,1,1),结算单号,货品名称,期初库存,入库数,出库数,结余库存
into #inv2
from #inv
order by 结算单号,货品名称
-- 结果
select * from #inv2 order by 序号
/*
序号 结算单号 货品名称 期初库存 入库数 出库数 结余库存
----------- ---------- ---------- ----------- ----------- ----------- -----------
1 100011 A 0 200 100 100
2 100011 B 0 50 20 30
3 100012 A 100 0 10 90
4 100012 B 30 0 0 30
5 100013 A 90 0 0 90
6 100013 B 30 0 5 25
(6 row(s) affected)
*/
-- 已有表生成机制是这样
create table #inv
(序号 int,结算单号 varchar(10),货品名称 varchar(10),期初库存 int,入库数 int,出库数 int,结余库存 int)
insert into #inv
select 1,'100011','A',0,200,100,100 union all
select 2,'100011','B',0,50,20,30 union all
select 3,'100012','A',100,0,10,90 union all
select 4,'100013','B',30,0,5,25
-- 补齐数据
update #inv set 序号=null
insert into #inv(结算单号,货品名称,期初库存,入库数,出库数,结余库存)
select 结算单号=a.结算单号,
货品名称=b.货品名称,
期初库存=isnull(d.结余库存,0),
入库数=0,
出库数=0,
结余库存=isnull(d.结余库存,0)
from (select distinct 结算单号 from #inv) a
cross join (select distinct 货品名称 from #inv) b
left join #inv c on a.结算单号=c.结算单号
and b.货品名称=c.货品名称
outer apply(select top 1 e.结余库存
from #inv e
where e.货品名称=b.货品名称
and e.结算单号<a.结算单号
order by e.结算单号 desc) d
where c.结算单号 is null
update a
set a.序号=b.rn
from #inv a
inner join (select rn=row_number() over(order by 结算单号,货品名称),
结算单号,
货品名称
from #inv) b on a.结算单号=b.结算单号 and a.货品名称=b.货品名称
-- 结果
select * from #inv order by 序号
/*
序号 结算单号 货品名称 期初库存 入库数 出库数 结余库存
----------- ---------- ---------- ----------- ----------- ----------- -----------
1 100011 A 0 200 100 100
2 100011 B 0 50 20 30
3 100012 A 100 0 10 90
4 100012 B 30 0 0 30
5 100013 A 90 0 0 90
6 100013 B 30 0 5 25
(6 row(s) affected)
*/
select ReportNO,ReportTime,LastReportNo,Client,PartName,Spec,Type,BeginBundle,InBundle,OutBundle,EndBundle,BeginQty,InQty,OutQty,EndQty,BeginCost,InCost,OutCost,EndCost
from Checkclient
where ID in(select max(b.ID) from Checkclient b where b.ReportNo<>'PR180411160310' group by b.Client,b.PartName,b.Spec)
and (Client+PartName+Spec) not in (Select Client+PartName+Spec from Checkclient where ReportNo='PR180411160310' )
Insert into 结算表(结算单号 货品名称 期初库存 入库数 出库数 结余库存)
select 结算单号 货品名称 期初库存 入库数 出库数 结余库存
from 结算表
where ID in(select max(b.ID) from 结算表b where b.结算单号 <>'100012'
and b.ID not in (Select c.ID from 结算表c where c. 货品名称=b. 货品名称)
group by 货品名称)
[/quote]
程序管原来运行也一样啊,只要你能拿到新结算单的结果集,最多当打个补丁加一段我上面逻辑的sql进去补没变动的货品库存就行了
Insert into 结算表(结算单号 货品名称 期初库存 入库数 出库数 结余库存)
select 结算单号 货品名称 期初库存 入库数 出库数 结余库存
from 结算表
where ID in(select max(b.ID) from 结算表b where b.结算单号 <>'100012'
and b.ID not in (Select c.ID from 结算表c where c. 货品名称=b. 货品名称)
group by 货品名称)