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 2005 Forums
 Transact-SQL (2005)
 How do I populate the nulls in each record?

Author  Topic 

shaunna
Starting Member

8 Posts

Posted - 2009-07-27 : 11:58:56
OK, this is way above my T-SQL skill level so I need some serious help. I'm assuming I'm going to have to do some kind of loop.

I have a table that will look like this - there will be any number of records because it's based on work items, and for each day column it shows the remaining hours (which vary of course depending on how much work has been done on each work item on a given day).

WorkItemID|DayOne|DayTwo|DayThree|DayFour|DayFive
111|8|Null|Null|6|3
222||10|8|6|Null|1
333|16|12|Null|Null|5
444||4|3|2|1|Null

The problem is wherever there is a null I need to grab the last real value for that work item. I never know how many work items I'll have or what days will have null values.

So for instance - on the first record with a WorkItemID of 111, I need to populate DayTwo and DayThree with 8 from DayOne.

Reason: Because there were 8 hours of work remaining on DayOne, and even though a work item wasn't worked on during DayTwo or DayThree, there are still 8 hours remaining that need to be carried over, so I need to populate those fields so I can sum them in a report.

Any help is greatly appreciated!

Shaunna

I have just enough knowledge to be dangerous. :)

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-07-27 : 12:35:29
UPDATE table
SET DayTwo = COALESCE(DayTwo,DayOne)
,DayThree = COALESCE(DayThree,DayTwo,DayOne)

etc.

Jim
Go to Top of Page

shaunna
Starting Member

8 Posts

Posted - 2009-07-27 : 15:09:09
Thank you! I believe that will work!

I have just enough knowledge to be dangerous. :)
Go to Top of Page
   

- Advertisement -