select 编码,年度,上年=(select sum(isnull(拨款,0)-isnull(花费,0)) from 临时表 where 编码=a.编码 and 年度<a.年度)
,拨款,花费,余额=(select sum(isnull(拨款,0)-isnull(花费,0)) from 临时表 where 编码=a.编码 and 年度<=a.年度)
from 临时表 a order by 编码,年度
,拨款,花费,余额=(select sum(isnull(拨款,0)-isnull(花费,0)) from 临时表 where 编码=a.编码 and 年度<=a.年度)
from 临时表 a order by 编码,年度
,拨款,花费,余额=(select sum(isnull(拨款,0)-isnull(花费,0)) from 临时表 where 编码=a.编码 and 年度<=a.年度)
from 临时表 a order by 编码,年度
(年度 int,编码 varchar(10),拨款 numeric(18,2), 花费 numeric(18,2))
年度 编码 上年结余 拨款 花费 余额
select 年度,
编码,
上年结余=isnull((select sum(拨款-花费)
from 临时表
where 年度<a.年度 and 编码=a.编码),0),
拨款,
花费,
余额=(select sum(拨款-花费)
from 临时表
where 年度<=a.年度 and 编码=a.编码)
from 临时表 a
编码,
上年结余=isnull((select sum(拨款-花费)
from 临时表
where 年度<a.年度 and 编码=a.编码),0),
拨款,
花费,
余额=(select sum(拨款-花费)
from 临时表
where 年度<=a.年度 and 编码=a.编码)
from 临时表 a
from 临时表 a