Sql 第一行某列减第二行某列
程序员文章站
2022-04-12 09:14:20
--1. 将结果插入临时表SELECT *INTO xxx FROM( SELECT TOP 1 a.FQTY,a.fseq FROM T_SAL_ORDERENTRY as a WHERE FQTY=5UNION ALL SELECT TOP 1 b.FQTY,b.fseq FROM T_SAL_ ......
--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