SQL Server 2012 - Update column based on formula

smackdun

I have a table data like this:

RowNo   Type   Formula      Amount
------------------------------------
  01     0                   3000
  02     0                   2000
  03     0                   1000
  04     1     01+02-03     

I'm trying to find a way to update the amount of the last row by formula based on value of previous rows.

The result should be like:

RowNo   Type   Formula      Amount
-----------------------------------
  01     0                   3000
  02     0                   2000
  03     0                   1000
  04     1     01+02-03      4000

Could you please suggest some solution? Thanks in advance!

Squirrel

Using Jeff Moden's CSV splitter DelimitedSplit8K

-- Setup table for testing
declare @tbl table
(
    RowNo   varchar(2),
    Type    int,
    Formula varchar(10),
    Amount  int
)
-- sample data
insert into @tbl select '01', 0, NULL, 3000
insert into @tbl select '02', 0, NULL, 2000
insert into @tbl select '03', 0, NULL, 1000
insert into @tbl select '04', 1, '01+02-03', NULL
insert into @tbl select '05', 1, '01-02+03', NULL

-- Query
select  *
from    @tbl t
    cross apply 
    (
        select  Amt = sum (i.Amount * f.Opr)
        from
        (
            select  ItemNumber, 
                Item = replace(replace(Item, '+', ''), '-', ''),
                Opr  = case when charindex('-', Item) > 0 then -1 else 1 end
            from    dbo.DelimitedSplit8K(replace(replace(Formula, '+', ',+'), '-', ',-'), ',') 
        ) f
        inner join @tbl i   on  f.Item  = i.RowNo
    ) a
where   t.Type  = 1

/* Result : 
RowNo Type        Formula    Amount      Amt
----- ----------- ---------- ----------- -----------
04    1           01+02-03   NULL        4000
05    1           01-02+03   NULL        2000

(2 row(s) affected)
*/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update rows based on rownumber in SQL Server 2012

From Dev

SQL Server 2012 Update table rate based on rate in table 2

From Dev

selecting random rows with normal distribution based on a column in SQL Server 2012

From Dev

selecting random rows with normal distribution based on a column in SQL Server 2012

From Dev

SQL Server - Select column to update based on another column value

From Dev

INVALID COLUMN? SQL Server 2012

From Dev

SQL Server 2012 - Update column if condition is met (CASE) else do not update

From Dev

Adding new column (with inner join) and insert (update) value with case when (SQL Server 2012)

From Dev

Using update with an inner join SQL SERVER 2012

From Dev

LIMIT equivalent for SQL Server 2012 in an UPDATE statement

From Dev

Query JSON inside SQL Server 2012 column

From Dev

SQL Server 2012 Invalid Column Name?

From Dev

SQL Server 2012 Identity column after a restore

From Dev

Get column name in SQL server 2012

From Dev

SQL Server 2012 Identity column after a restore

From Dev

Merge data into one column - sql server 2012

From Dev

PIVOT the date column in SQL Server 2012

From Dev

Specify a computed column in SQL Server 2012 as an average

From Dev

SQL Server 2012 query XML column

From Dev

Update Column using Row Number based on primary key column in SQL Server

From Dev

Atomic update column in SQL Server

From Dev

XML column update in SQL Server

From Dev

Oracle Linked Server in SQL Server 2012 - query with DATE column not working

From Dev

SQL Server: SUM of a column based on another column

From Dev

SQL Server column max based on another column

From Dev

Convert SQL cell value to column names in SQL Server 2012

From Dev

Convert SQL cell value to column names in SQL Server 2012

From Dev

How to write this SQL Server syntax to update a column based on the values of 2 other columns in that same table

From Dev

Update SQL Table based on column values

Related Related

  1. 1

    Update rows based on rownumber in SQL Server 2012

  2. 2

    SQL Server 2012 Update table rate based on rate in table 2

  3. 3

    selecting random rows with normal distribution based on a column in SQL Server 2012

  4. 4

    selecting random rows with normal distribution based on a column in SQL Server 2012

  5. 5

    SQL Server - Select column to update based on another column value

  6. 6

    INVALID COLUMN? SQL Server 2012

  7. 7

    SQL Server 2012 - Update column if condition is met (CASE) else do not update

  8. 8

    Adding new column (with inner join) and insert (update) value with case when (SQL Server 2012)

  9. 9

    Using update with an inner join SQL SERVER 2012

  10. 10

    LIMIT equivalent for SQL Server 2012 in an UPDATE statement

  11. 11

    Query JSON inside SQL Server 2012 column

  12. 12

    SQL Server 2012 Invalid Column Name?

  13. 13

    SQL Server 2012 Identity column after a restore

  14. 14

    Get column name in SQL server 2012

  15. 15

    SQL Server 2012 Identity column after a restore

  16. 16

    Merge data into one column - sql server 2012

  17. 17

    PIVOT the date column in SQL Server 2012

  18. 18

    Specify a computed column in SQL Server 2012 as an average

  19. 19

    SQL Server 2012 query XML column

  20. 20

    Update Column using Row Number based on primary key column in SQL Server

  21. 21

    Atomic update column in SQL Server

  22. 22

    XML column update in SQL Server

  23. 23

    Oracle Linked Server in SQL Server 2012 - query with DATE column not working

  24. 24

    SQL Server: SUM of a column based on another column

  25. 25

    SQL Server column max based on another column

  26. 26

    Convert SQL cell value to column names in SQL Server 2012

  27. 27

    Convert SQL cell value to column names in SQL Server 2012

  28. 28

    How to write this SQL Server syntax to update a column based on the values of 2 other columns in that same table

  29. 29

    Update SQL Table based on column values

HotTag

Archive