eclare @a table (收入 int, 付出 int , 余额 int)insert into @a 
select  15,    13 , 0
union all
select  12,    10,0
union
select   9,      10,0declare @i int
set @i = 0
update @a set @i = 收入 - 付出 + @i , 余额 = @i
select * from @a

解决方案 »

  1.   

    declare @a table (收入 int, 付出 int , 余额 int)insert into @a 
    select  15,    13 , 0
    union all
    select  12,    10,0
    union all
    select   9,      10,0declare @i int
    set @i = 0
    update @a set @i = 收入 - 付出 + @i , 余额 = @i
    select * from @a
      

  2.   

    select 收入,付出,收入-付出 as 余额 from table
      

  3.   

    ---测试脚本
    -------------------------------------
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[money]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[money]
    GOCREATE TABLE [dbo].[money] (
    [a] [bigint] NULL ,
    [b] [bigint] NULL 
    ) ON [PRIMARY]
    GOinsert into money(a,b) values(90,70)
    insert into money(a,b) values(100,90)select a,b,a-b as c from moneydrop table money
    -------------------------------------------
      

  4.   

    forjzforjz() 
    你这样的话,只能是余额了,而我要的是余额不是这样的,你看一下
      

  5.   

    你的表里有排序字段吗,如果有,我的写法是:
    create table fc_sum
    (
     收入 int,
     付出 int,
     idh  int 

    insert into fc_sum(收入 , 付出) values (15,13,1)
     
    insert into fc_sum(收入 , 付出) values (12,10,2)
    insert into fc_sum(收入 , 付出) values (9,10,3)*/select t.收入 ,t.付出,(select sum(收入-付出) from fc_sum where idh<=t.idh) as 合计
    from fc_sum t
      

  6.   

    ---测试脚本
    -------------------------------------
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[moneyTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[moneyTable]
    GOCREATE TABLE [dbo].[moneyTable] (
    [Mid] [bigint] IDENTITY (1, 1) NOT NULL ,
    [a] [bigint] NULL ,
    [b] [bigint] NULL 
    ) ON [PRIMARY]
    GOinsert into moneyTable(a,b) values(90,70)
    insert into moneyTable(a,b) values(100,90)
    insert into moneyTable(a,b) values(54,63)select a,b,(select sum(a)-sum(b) from moneyTable as money1 where money2.mid>=money1.mid) as 余额 from moneyTable as money2drop table moneyTable
    -------------------------------------------