Sql 第一行某列减第二行某列

–1. 将结果插入临时表
select
*
into xxx
from(
select top 1 a.fqty,a.fseq
from t_sal_orderentry as a
where fqty=5
union all
select top 1 b.fqty,b.fseq
from t_sal_orderentry as b
where fqty=1
) as c

select * from xxx

–最初update
— update xxx
— set fqty = isnull((
— select top 1 a.fqty
— from xxx as a
— where t.fseq >= fseq
— and fqty=5
— )-(
— select top 1 b.fqty
— from xxx as b
— where t.fseq <fseq
— and fqty=1
— ),0)
— from xxx as t

–将记录1减记录2 ,新增至第3行
insert into xxx
select isnull((
select top 1 a.fqty
from xxx as a
where t.fseq >= fseq
and fqty=5
)-(
select top 1 b.fqty
from xxx as b
where t.fseq <fseq
and fqty=1
),0),t.fseq
from xxx as t
where fseq=1

select * from xxx

drop table xxx

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐