SQL 查询一个结果,包含数量1,数量2,数量3 三列,sql如何实现:当第一行数量2>数量1时,超过的数量加入第二行的数量2,当第二行的数量2>数量1时,多出数量加入第三行,以此类推。。。
够复杂的啊 这得用循环吧
建议采用存储过程做吧。如果用SQL太复杂了。如果包含数量2<数量1不累加到第三行的话,用SQL还不一定能实现。
可以这样
update T_A
set A = (select A from T_A where id = 1) - isnull((select sum(B) from T_A b where b.id < T_A.id),0),
c = (select A from T_A where id = 1) - (select sum(B) from T_A b where b.id <= T_A.id)
可以这样
update T_A
set A = (select A from T_A where id = 1) - isnull((select sum(B) from T_A b where b.id < T_A.id),0),
c = (select A from T_A where id = 1) - (select sum(B) from T_A b where b.id <= T_A.id)