select line,qty,qty1,qty-qty1 qty2 from table1 order by line
解决方案 »
- 多表组合查询去掉重复行
- 关于sql查询的问题???
- update语句,如何使用order by子句
- 如何在一个表中update一条日期最大的记录?
- 请问 sql server 2000 的 个人版 和 企业版 的主要 区别在哪里????
- 如何把输入的记录转换为SQL语句
- 求一SQL函数,根据参数,返回xml结构
- 请教:关于ERP后台SQL服务的问题。
- 两个表的连接查询
- 请教:PB直连sql server2k,编成exe后,拿别的机器上运行,连不上库 说:Db_library network communications layer not loaded
- 如何防止mdf文件被随意附加,如何保护mdf文件?
- 各位大侠请帮忙看一下如何避免有空值。
declare @i int
set @i=0
update table1 set qty2=qty-qty1-@i,@i=@i+qty1
select * from #tmp
a.qty,
a.qty1,
qty2 = a.qty - (select IsNull(sum(qty1),0) from @t where line <= a.line)
from @t a
declare @t table(line varchar(2),qty decimal(5,2),qty1 decimal(5,2))
insert into @t select '2',100,55
union all select '5',100,10
union all select '6',100,22
union all select '8',100,35
--查询
select line,
qty,
qty1,
qty2=qty-(select sum(qty1) from @t where line<=a.line)
from @t a
--结果
line qty qty1 qty2
---- ------- ------- ----------------------------------------
2 100.00 55.00 45.00
5 100.00 10.00 35.00
6 100.00 22.00 13.00
8 100.00 35.00 -22.00(所影响的行数为 4 行)