@@Version 1
Using SQL Server 2008, I am trying to cascade values down a column. I have a table with group id (GID) and Seq containing ordering for records within the group. For the columns present, in this case Name and Salary - my real table has over 50 columns, if they contain NULLs I need to update the NULL value with the value from the previous row for that column that contain a non-null value.
Here is something to illustrate this:
GID Seq Name Salary
1 1 James NULL
1 2 NULL 100
1 3 NULL NULL
2 1 NULL 81
2 2 Smith NULL
2 3 NULL NULL
3 1 Charles NULL
3 2 NULL NULL
3 3 Brown NULL
3 4 NULL 75
4 0 Ron 50
4 1 NULL 20
4 2 NULL NULL
My result should be:
GID Seq Name Salary
1 1 James NULL
1 2 James 100
1 3 James 100
2 1 NULL 81
2 2 Smith 81
2 3 Smith 81
3 1 Charles NULL
3 2 Charles NULL
3 3 Brown NULL
3 4 Brown 75
4 0 Ron 50
4 1 Ron 20
4 2 Ron 20
I am looking to do this without using dynamic SQL, loops or cursors.
Code for simple test case:
DECLARE @Test TABLE (GID int, Seq int, Name varchar(50), Salary decimal)
INSERT INTO @Test VALUES (1, 1, 'James', NULL)
INSERT INTO @Test VALUES (1, 2, NULL, 100.40)
INSERT INTO @Test VALUES (1, 3, NULL, NULL)
INSERT INTO @Test VALUES (2, 1, NULL, 80.50)
INSERT INTO @Test VALUES (2, 2, 'Smith', NULL)
INSERT INTO @Test VALUES (2, 3, NULL, NULL)
INSERT INTO @Test VALUES (3, 1, 'Charles', NULL)
INSERT INTO @Test VALUES (3, 2, NULL, NULL)
INSERT INTO @Test VALUES (3, 3, 'Brown', NULL)
INSERT INTO @Test VALUES (3, 4, NULL, 75)
INSERT INTO @Test VALUES (4, 0, 'Ron', 50)
INSERT INTO @Test VALUES (4, 1, NULL, 20)
INSERT INTO @Test VALUES (4, 2, NULL, NULL)
SELECT * FROM @Test
@@Version 2 Thanks GilM for the solution to @@Version 1. I have made a small addition to the problem. The starting number in the Seq column may be either a 0 or 1. In the solution to the first problem the anchor in the recursive CTE refers to 1, what if its either a 1 or 0? The last 3 rows of data (GID = 4) were added to all the above three code blocks in this version.
Thanks!
How about this?:
With 50 columns there will be a lot of typing and a lot of correlated sub-queries.
Here is a version that uses XML instead. Less typing and performance could be better.
SE-Data