简体   繁体   English

如果前一列值不为空,则更新表中的下一列

[英]Update next column in table if previous column value is not null

When a person receives a score, an entry is added into the table @uniqueScores :当一个人收到分数时,会在表中添加一个条目@uniqueScores

Pid | Date | Score

I have a stored procedure returning a table @people with the score columns containing the data from @uniqueScores (that fall within the past 3 months)我有一个存储过程返回一个表@people其中的分数列包含来自@uniqueScores的数据(过去 3 个月内)

Pid | S1 | S2 | S3 | S4 | S5

I have a small test dataset, however I'm having trouble getting any scores beyond the first score registered to a user to appear in Score2 or beyond.我有一个小的测试数据集,但是我无法获得超出用户注册的第一个分数以显示在 Score2 或更高版本中的任何分数。

Here is my test dataset这是我的测试数据集

Pid | Date       | Score
 #1 | 2020/07/01 | 8
 #1 | 2020/09/15 | 8
 #2 | 2020/09/21 | 3
 #3 | 2020/10/01 | 5
 #4 | 2020/10/18 | 6
 #4 | 2020/10/31 | 2

My update statement, to update the Person column with the data我的更新语句,用数据更新 Person 列

BEGIN
  UPDATE @people
  SET    [Score5] = (CASE WHEN ( [p].[Score4] is not null and [p].[Score5] is null ) THEN [us].[Score] ELSE NULL END)
         ,[Score4] = (CASE WHEN ( [p].[Score3] is not null and [p].[Score4] is null ) THEN [us].[Score] ELSE NULL END)
         ,[Score3] = (CASE WHEN ( [p].[Score2] is not null and [p].[Score3] is null ) THEN [us].[Score] ELSE NULL END)
         ,[Score2] = (CASE WHEN ( [p].[Score1] is not null and [p].[Score2] is null ) THEN [us].[Score] ELSE NULL END)
         ,[Score1] = (CASE WHEN ( [p].[Score1] is null ) THEN [us].[Score] ELSE NULL END)
  FROM   @people [p] inner join  @uniqueScores [us]
         on [p].[PersonID] = [us].[PersonID]
  WHERE  [Date] >= @DateLimit -- within the previous 3 months
END

However, the query isn't updating the table with any but the first eligible values.但是,查询不会使用第一个符合条件的值更新表。 The returned table looks like this返回的表看起来像这样

Pid | S1 | S2   | S3   | S4   | S5
#1  | 8  | null | null | null | null
#2  | 3  | null | null | null | null
#3  | 5  | null | null | null | null
#4  | 6  | null | null | null | null

The first table entry which is ineligible to be considered for the table isn't included which is great, however Person #4's second score is also missing.第一个不符合被考虑参加表格的表格条目不包括在内,这很好,但是第 4 人的第二个分数也丢失了。

I've been looking at PIVOT , WHILE and a CURSOR but I've got no closer to making this work.我一直在研究PIVOTWHILECURSOR但我还没有接近完成这项工作。 I'm sure I've missed something simple however I just can't see it.我确定我错过了一些简单的东西,但是我看不到它。

UPDATE updates each row once . UPDATE更新每一行一次 Preaggregate for multiple updates:为多个更新预聚合:

UPDATE p
    SET Score1 = us.score_1,
        Score2 = us.score_2,
        Score3 = us.score_3,
        Score4 = us.score_4,
        Score5 = us.score_5
    FROM @people [p] inner join
         (SELECT us.PersonID,
                 MAX(CASE WHEN seqnum = 1 THEN Score END) as score_1,
                 MAX(CASE WHEN seqnum = 2 THEN Score END) as score_2,
                 MAX(CASE WHEN seqnum = 3 THEN Score END) as score_3,
                 MAX(CASE WHEN seqnum = 4 THEN Score END) as score_4,
                 MAX(CASE WHEN seqnum = 5 THEN Score END) as score_5
          FROM (SELECT us.*,
                       ROW_NUMBER() OVER (PARTITION BY PersonId ORDER BY Date) as seqnum
                FROM @uniqueScores us
                WHERE [Date] >= @DateLimit -- within the previous 3 months
               ) us
          GROUP BY us.PersonID
         ) s
         ON us.PersonID = p.PersonId;

Note: You don't specify what order you want the scores in. This puts the oldest ones first.注意:您没有指定分数的顺序。这会将最旧的放在最前面。 Use ORDER BY DESC if you want the newer ones first.如果您首先想要较新的,请使用ORDER BY DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM