关于存款账目sql语句????
表记录如下:
A 50
B 30
A 10
B 15
C 35
B 40
就是每个人存入一笔款,有一条记录:
现在要把a、b的账户同时扣掉60,怎么写sql语句呢???
[解决办法]
存款没有时间吗
[解决办法]
- SQL code
update a set col2=case when (select sum(col2) from tb where col1=a.col1 and 时间<=a.时间)<=60 then 0 else col2-(select sum(col2) from tb where col1=a.col1 and 时间<=a.时间) endfrom tb a where col1 in ('a','b')
[解决办法]
- SQL code
if object_id('tb','U') is not null drop table tbgocreate table tb( c1 varchar(10), c2 int)goinsert into tbselect 'A',50 union allselect 'B',30 union allselect 'A',10 union allselect 'B',15 union allselect 'C',35 union allselect 'B',40gowith cte as(select c1,c2,row=row_number() over(order by getdate()) from tb)update a set c2=b.c2 from (select *,row=row_number() over(order by getdate()) from tb) a inner join (select c1,row, c2=case when (select sum(c2) from cte where c1=t1.c1 and row<=t1.row)-60<0 then 0 else (select sum(c2) from cte where c1=t1.c1 and row<=t1.row)-60 end from cte t1 where c1='B' or C1='A') b on a.row=b.row/*c1 c2---------- -----------A 0B 0A 0B 0C 35B 25(6 行受影响)*/
[解决办法]
把表结构和数据列出来,还有你想要的结果也列出来
[解决办法]
在之后往里插入数据的时候要做一个插入的判断:
- SQL code
--对已有的 id xtype 类型要做累加或减update tbset num = num + @valuewhere id = @id and xtype = @xtype--@value是插入的num值,@id @xtype 是表中对应的id xtype--对没有在表中存在的id xtype可以直接插入insert into tbselect @id,@value,@xtype,@stime
[解决办法]
4楼的不就是你想要的结果吗
[解决办法]