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.
| Author |
Topic |
|
Littleterry
Starting Member
9 Posts |
Posted - 2008-08-27 : 09:52:39
|
| I have the following table and I want to write a query using the WHILE loop, to update the blanks in the table to nearest nValue that came before it with (in the result set) without updating the table.Table-----Date pValue nValue1/1/2008 3 51/2/2008 5 11/3/2008 6 null1/4/2008 8 null1/5/2008 2 null1/6/2008 3 81/7/2008 4 null1/8/2008 6 null1/9/2008 8 41/10/2008 9 nullHelp Plz Terry |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 09:58:10
|
| Are you using SQL 2005 or 2000? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 10:00:11
|
| [code]SELECT t.Date, t.pValue, COALESCE(t.nValue,(SELECT TOP 1 nValue FROM YourTable WHERE Date<t.Date ORDER BY Date DESC))FROM YourTable t[/code] |
 |
|
|
Littleterry
Starting Member
9 Posts |
Posted - 2008-08-27 : 10:10:08
|
| I'm Using SQL 2005 - Is there any way to do it using a "while loop" |
 |
|
|
Littleterry
Starting Member
9 Posts |
Posted - 2008-08-27 : 10:14:39
|
| This is the results i want achieve ------Date pValue nValue1/1/2008 3 51/2/2008 5 11/3/2008 6 11/4/2008 8 11/5/2008 2 11/6/2008 3 81/7/2008 4 81/8/2008 6 81/9/2008 8 41/10/2008 9 4 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-27 : 10:19:20
|
quote: Originally posted by Littleterry I'm Using SQL 2005 - Is there any way to do it using a "while loop"
then use thisSELECT t.Date,t.pValue,COALESCE(t.nValue,t1.nValue)FROM YourTable tOUTER APPLY(SELECT Top 1 nValue FROM YourTable WHERE Date < t.Date ORDER BY Date DESC) t1 |
 |
|
|
Littleterry
Starting Member
9 Posts |
Posted - 2008-08-27 : 16:57:53
|
| Didn't work i got back nulls |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-28 : 03:48:36
|
| [code]SELECT t.Date,t.pValue,COALESCE(t.nValue,t1.nValue)FROM YourTable tOUTER APPLY(SELECT Top 1 nValue FROM YourTable WHERE Date < t.Date AND nValue IS NOT NULL ORDER BY Date DESC) t1[/code] |
 |
|
|
|
|
|