Update null column value from non null value in pr

2020-02-09 17:58发布

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

2条回答
贼婆χ
2楼-- · 2020-02-09 18:19

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
查看更多
smile是对你的礼貌
3楼-- · 2020-02-09 18:26
update T set
  Name =   (
           select top(1) T1.Name
           from @Test as T1
           where T1.GID = T.GID and
                 T1.Seq <= T.Seq and
                 T1.Name is not null
           order by T1.Seq desc
           ),
  Salary = (
           select top(1) T1.Salary
           from @Test as T1
           where T1.GID = T.GID and
                 T1.Seq <= T.Seq and
                 T1.Salary is not null
           order by T1.Seq desc
           )
from @Test as T
where T.Name is null or 
      T.Salary is null

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.

with C as
(
  select GID,
         (
         select *
         from @Test as T2
         where T1.GID = T2.GID
         order by T2.Seq desc
         for xml path('row'), type
         ) as X
  from @Test as T1
  group by GID
)
update T set
       Name   = C.X.value('(/row[Seq<=sql:column("T.Seq")]/Name)[1]',   'varchar(50)'),
       Salary = C.X.value('(/row[Seq<=sql:column("T.Seq")]/Salary)[1]', 'decimal')
from @Test as T
  inner join C 
    on T.GID = C.GID

SE-Data

查看更多
登录 后发表回答