update T set areadypay=(select sum(price) from T A where A.id=T.id and A.[datetime]<=T.[datetime])
declare @tb table (id int, dates datetime, price int,leaving int)insert into @tb select 6, '1990-1-1', 100.0, 900.0 union all select 6, '1990-2-1', 100.0, 900.0 select id,dates, price,leaving,(select sum(price) from @tb where id=t.id and dates<=t.dates) as sumprice from @tb t
set areadypay=(select sum(price) from T A where A.id=T.id and A.[datetime]<=T.[datetime])
declare @tb table (id int, dates datetime, price int,leaving int)insert into @tb select 6, '1990-1-1', 100.0, 900.0 union all
select 6, '1990-2-1', 100.0, 900.0 select id,dates, price,leaving,(select sum(price) from @tb where id=t.id and dates<=t.dates) as sumprice from @tb t
insert into tb values(6, '1990-1', 100.0, 900.0)
insert into tb values(6, '1990-2', 100.0, 800.0)
goselect * , areadypay = (select sum(price) from tb where datetime <= t.datetime) from tb tdrop table tb/*
id datetime price leaving areadypay
----------- ---------- ----------- ----------- -----------
6 1990-1 100 900 100
6 1990-2 100 800 200(所影响的行数为 2 行)
*/