@@ Version 1
Using SQL Server 2008, I am trying to cascade values ββby column. I have a table with a group identifier (GID) and Seq containing the ordering of the records in the group. For the columns present, in this case, Name and Salary - my real table contains more than 50 columns, if they contain NULL, I need to update the NULL value from the previous row for this column that contains a non-zero value.
Here are some things to illustrate:
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 want to do this without using dynamic SQL, loops or cursors.
Code for a 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 to GilM for solving @@ Version 1. I made a small addition to the problem. The starting number in the Seq column can be either 0 or 1. In solving the first problem, the anchor in the recursive CTE refers to 1, what if its either 1 or 0? The last 3 lines of data (GID = 4) have been added to all three code codes in this version.
Thanks!