Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 updating rows based on upper row value

Author  Topic 

raj.prabhu001
Starting Member

16 Posts

Posted - 2012-04-06 : 23:52:47
i have to update rows for a table of col1 based value in upper row.
if the row is null or balnk or 0 then it should be updated with value of upper row i.e. previous row value. as shown below

col1
-----
12
0
45
56

12

23

so in the above case 0 value shoud be upadted with 12 and
rows which are blank below 56 value should be upadated with 56 and so on.

please suggest the query for the same.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-06 : 23:57:51
do you've an id field to determine the order? without specifying in terms of column, there's no concept of order in sql table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raj.prabhu001
Starting Member

16 Posts

Posted - 2012-04-07 : 00:04:29
we are not having any id field row but you can add new unique column.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-07 : 00:13:49
what do you mean by you can add? so currently table doesnt have unique valued column?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

raj.prabhu001
Starting Member

16 Posts

Posted - 2012-04-07 : 01:14:03
yes there customerid column but it is not ordered in the database.
Go to Top of Page

namman
Constraint Violating Yak Guru

285 Posts

Posted - 2012-04-07 : 22:07:33
As visakh16 say, we must have a column indecating order of rows. Without that, we dont know which one is previous row, previous row of 0 may be 45, not 12.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-08 : 18:08:31
quote:
Originally posted by raj.prabhu001

yes there customerid column but it is not ordered in the database.


then there's no way to guarantee the order

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -