答案 0 :(得分:1)
update T
set bus_day_mtd = (
select sum(bus_day) from T t2 where t2.ymd <= T.ymd
--or cast(bus_day_flag as int) if referencing the bit column
)
您是否希望动态计算而不是静态列?
答案 1 :(得分:1)
在SQL Server 2012+中,您可以使用累积总和来执行此操作:
with toupdate as (
select t.*,
sum(case when bus_day_flag = 1 then bus_day end) over (order by ymd) as new_bus_day_mtd
from t
)
update toupdate
set bus_day_mtd = new_bus_day_mtd;