Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

sql server 2008 - Update null column value from non null value in previous row

@@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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

How about this?:

;WITH CTE AS (
SELECT GID, SEQ, Name, Salary
FROM @Test t1
WHERE SEQ = (SELECT MIN(SEQ) FROM @Test t2 WHERE t2.GID = t1.GID)
UNION ALL
SELECT t.GID, t.SEQ, COALESCE(t.Name,c.Name), COALESCE(t.Salary,c.Salary)
FROM CTE c
JOIN @Test t ON t.GID = c.GID AND t.SEQ = c.SEQ+1
)
UPDATE t SET 
    Name = c.Name,
    Salary =  c.Salary
FROM @Test t
JOIN CTE c ON c.GID = t.GID AND c.Seq = t.SEQ

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...