| Author |
Topic |
|
VijayV
Starting Member
4 Posts |
Posted - 2011-11-09 : 14:18:39
|
| There is a requirement to replace the NULLS in the table and the it has to be done only if certain conditions meet.The data in the table is for every 15 minutes in a given day and it looks somewhat like thisHGWY DISTANCE 0000 0015 0030 0045 0100 0115......1 69.27 45 68 652 69.6 68 NULL 453 69.98 60 NULL 554 70.34 NULL 65 35 5 70.68 67 72 56 6 70.97 65 62 NULL7 74.41 70 80 75*The NULL value needs to be replaced only if the difference in distance between previous and next NON NULL value is less than 2.*The NULL value should be replaced by the average of NEXT and PREVIOUS NON NULL Values.so for the above the output should look like thisHGWY DISTANCE 0000 0015 0030 1 69.27 45 68 652 69.6 68 66.5(68+65/2) 453 69.98 60 66.5 (68+65/2) 554 70.34 63.5(60+67/2) 65 35 5 70.68 67 72 56 6 70.97 65 62 NULL7 74.41 70 80 75The NULL value in column 0030 was not replaced because the difference in distance between HGWY 7 and 5 was > 2(74.41-70.98 = 3.43)Since in a given day there are 96 15 minutes intervals or 96 columns in total, the performance is really an issue.Thanks for help in advance |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
VijayV
Starting Member
4 Posts |
Posted - 2011-11-09 : 18:30:02
|
Brett,The rows are ordered in ascending order of Distance.HGWY DISTANCE 0000 0015 0030....1 69.27 452 69.6 683 69.98 604 70.34 NULL ---(60+67/2)=63.55 70.68 676 70.8 657 70.97 658 71.5 NULL --Should not be replaced since distance (75.0 - 70.97 > 2)9 73.0 NULL --Should not be replaced since distance (75.0 - 70.97 > 2)10 75.0 70 The NULL in 0000 has to be replaced with Average of previous NON NULL value in the same column (60) and NEXT NON NULL value (67), so 60 + 67/2 = 63.5. Also the difference in distances should be less than 2. For the first NULL the difference in Distance is 70.68 - 69.98 < 2. So, should be replacedThe NULLS in second case should not be replaced since difference in Distances for NON NULL values 75.0 - 70.97 > 2.I hope i was clear. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-11-09 : 20:44:47
|
| This is meaningless..but I'm sure you could do itSo find all the null values, then for each null row, find the first non null row that is less than your miles, then find the first non null value that is above that..right?Then applyyor less than 2 row and apply you r calculationRight???No freaking meaningWho is asking you to do this, or is it a class homework assignment, or did you think of this on your own?What Value does this derived data have by the way??Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-10 : 00:57:39
|
| [code]UPDATE tSET [0000]= (t1.[0000] + t2.[0000])/2.0FROM table tCROSS APPLY (SELECT TOP 1 DISTANCE,[0000] FROM table WHERE DISTANCE < t.DISTANCE AND [0000] IS NOT NULL ORDER BY DISTANCE DESC)t1CROSS APPLY (SELECT TOP 1 DISTANCE,[0000] FROM table WHERE DISTANCE > t.DISTANCE AND [0000] IS NOT NULL ORDER BY DISTANCE ASC)t2WHERE [0000] IS NULLAND (t2.DISTANCE - t1.DISTANCE) < 2.00[/code]similar logic can be applied for others too------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
VijayV
Starting Member
4 Posts |
Posted - 2011-11-10 : 09:30:49
|
| Thanks Visakh that worked for me. Appreciate your help. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-11-10 : 09:39:10
|
| wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
VijayV
Starting Member
4 Posts |
Posted - 2011-11-10 : 13:41:19
|
| Brett,The values captured in that table are the average speeds along a highway and the distances are the exists. The restriction distance < 2 was applied not to replace NULL with an average value from exits far apart. Thanks |
 |
|
|
|