Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2003
    Posts
    1

    Post Unanswered: Update a row based on value of pervious row

    Hi,
    Sorry if it sounds too simple question, but I need your suggestion:

    I have got a table tblWork

    Three columns (workID, WorkPay, CumulativePayTodate)

    They Look like:
    workID WorkPay CumulativePayTodate
    -------- -------- ------------------
    0 100
    1 300
    2 200
    4 150
    .................

    After Update, It should look like

    workID WorkPay CumulativePayTodate
    -------- -------- ------------------
    0 100 0
    1 300 100 (value of previous workpay)
    2 200 400 (value of 100+300 workpay)
    4 150 600

    ------------------


    Any suggestion will be really appriciated. Thanks Lot
    -Mk

  2. #2
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621
    drop table table1
    go
    create table table1(id int
    ,m1 money
    ,m2 money)
    go
    insert table1 select 1,10,0
    insert table1 select 2,20,0
    insert table1 select 3,85,0
    insert table1 select 4,12,0
    insert table1 select 5,65,0
    insert table1 select 6,12,0
    insert table1 select 7,1,0
    go
    update table1 set m2=(select sum(m1) from table1 where id<a.id)
    from table1 a
    select * from table1

    Use isnull for subquery if needs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •