Update based on previous value SQL SERVER 2005

gcbs_fln

I need to Update these NULL Values:

PK |  CODE
---+-------
1  |   20
2  |   NULL
3  |   NULL
4  |   30
5  |   NULL
6  |   NULL
7  |   NULL
8  |   40
9  |   NULL

Like this:

PK   |   CODE
-----+------------
1    |    20
2    |    20
3    |    20
4    |    30
5    |    30
6    |    30
7    |    30
8    |    40
9    |    40

It should always be based on the last minimum value.

I have tried the code below, but it just updates the first row before the one who had value at the beginning.

QUERY

UPDATE TT 
SET CODE = (SELECT CODE 
FROM #TSPV_TEMP T2 with(nolock)
WHERE T2.KEY = (tt.KEY -1))
FROM #TSPV_TEMP TT with (nolock)
WHERE tt.CODE IS NULL
Gordon Linoff

You can do this as:

UPDATE TT 
    SET CODE = (SELECT TOP 1 CODE
                FROM #TSPV_TEMP T2 with(nolock)
                WHERE T2.KEY < tt.KEY AND
                      CODE IS NOT NULL
                ORDER BY KEY DESC
               )
    FROM #TSPV_TEMP TT with (nolock)
    where tt.CODE IS NULL;

Note the differences in the subquery. This looks for the previous non-NULL value for CODE for the update.

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 based on a select statement SQL Server 2005

From Dev

SQL Server update value if previous value is not null

From Dev

Key Value Pair XML (SQL Server 2005)

From Dev

SQL Server 2005 : If statement with return value

From Dev

Change Increment value for Identity - SQL Server 2005

From Dev

SQL Server 2005 - Insert with Select for 1 Value

From Dev

VBScript and SQL Server 2005 compare current row to previous row

From Dev

VBScript and SQL Server 2005 compare current row to previous row

From Dev

Update value based on the previous updated value mysql

From Dev

How To Update The Date Based On Previous Record Value

From Dev

How To Update The Date Based On Previous Record Value

From Dev

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

From Dev

SQL: how to update a row based on a previous row

From Dev

Get median value of a time period in SQL Server 2005, 2008 server

From Dev

How to update this query without looping or cursor in SQL Server 2005

From Dev

SQL Server 2005 - writing an insert and update trigger for validation

From Dev

How to update this query without looping or cursor in SQL Server 2005

From Dev

Encryption SQL Server 2005

From Dev

How to find lowest count of rows for a value in SQL Server 2005

From Dev

Subquery returned more than 1 value. in SQL Server 2005

From Dev

How to minus current and previous value in SQL Server

From Dev

SQL 2005 Update Column on Insert

From Dev

How to update SQL column based on previous search result of the same table

From Dev

SQL - Update Record Based on Entity's Previous Record

From Dev

Dynamic PIVOT in SQL Server 2005

From Dev

Cube Creation in SQL Server 2005

From Dev

order by case in sql 2005 server

From Dev

SQL Server 2005 database comparison

From Dev

order by case in sql 2005 server

Related Related

  1. 1

    Update based on a select statement SQL Server 2005

  2. 2

    SQL Server update value if previous value is not null

  3. 3

    Key Value Pair XML (SQL Server 2005)

  4. 4

    SQL Server 2005 : If statement with return value

  5. 5

    Change Increment value for Identity - SQL Server 2005

  6. 6

    SQL Server 2005 - Insert with Select for 1 Value

  7. 7

    VBScript and SQL Server 2005 compare current row to previous row

  8. 8

    VBScript and SQL Server 2005 compare current row to previous row

  9. 9

    Update value based on the previous updated value mysql

  10. 10

    How To Update The Date Based On Previous Record Value

  11. 11

    How To Update The Date Based On Previous Record Value

  12. 12

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

  13. 13

    SQL: how to update a row based on a previous row

  14. 14

    Get median value of a time period in SQL Server 2005, 2008 server

  15. 15

    How to update this query without looping or cursor in SQL Server 2005

  16. 16

    SQL Server 2005 - writing an insert and update trigger for validation

  17. 17

    How to update this query without looping or cursor in SQL Server 2005

  18. 18

    Encryption SQL Server 2005

  19. 19

    How to find lowest count of rows for a value in SQL Server 2005

  20. 20

    Subquery returned more than 1 value. in SQL Server 2005

  21. 21

    How to minus current and previous value in SQL Server

  22. 22

    SQL 2005 Update Column on Insert

  23. 23

    How to update SQL column based on previous search result of the same table

  24. 24

    SQL - Update Record Based on Entity's Previous Record

  25. 25

    Dynamic PIVOT in SQL Server 2005

  26. 26

    Cube Creation in SQL Server 2005

  27. 27

    order by case in sql 2005 server

  28. 28

    SQL Server 2005 database comparison

  29. 29

    order by case in sql 2005 server

HotTag

Archive