id   datetime  price  leaving
6 1990-1 100.0 900.0
6 1990-2 100.0 800.0
怎样得到下表
id  datetime price leaving areadypay6 1990-1 100.0 900.0 100.0
6 1990-2 100.0 800.0 200.0
就是说 结果的最后一列是price列的累加

解决方案 »

  1.   

    update T
    set areadypay=(select sum(price) from T A where A.id=T.id and A.[datetime]<=T.[datetime])
      

  2.   


    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
      

  3.   

    create table tb(id int,  datetime varchar(10) , price int, leaving int)
    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 行)
    */