0

请找到吹瓶台。

桌子

ClientID    Balance1    Balance2    Balance3    Balance4    Balance5    Balance6    Balance7    Balance8    Balance9
A           NULL        NULL        NULL        3           Null        Null        Null        Null        Null
B           10          null        Null        Null        20          Null        Null        Null        NULL
C           Null        8           Null        10          Null        Null        1           Null        NULL
D           Null        19          Null        Null        Null        Null        Null        Null        NULL
E           Null        Null        50          Null        Null        Null        Null        Null        NULL
F           NULL        NULL        NULL        30          NULL        NULL        NULL        NULL        NULL    
G           NULL        NULL        NULL        NULL        80          NULL        NULL        NULL        NULL      

如何使用查询将上表修改为如下所示,请帮助!

ClientID    Balance1    Balance2    Balance3    Balance4    Balance5    Balance6    Balance7    Balance8    Balance9
A           NULL        NULL        NULL        3           3           3           3           3           3   
B           10          10          10          10          20          20          20          20          20
C           Null        8           8           10          10          10          1           1           1
D           Null        19          19          19          19          19          19          19          19
E           Null        Null        50          50          50          50          50          50          50          
F           NULL        NULL        NULL        30          30          30          30          30          30          
G           NULL        NULL        NULL        NULL        80          80          80          80          80          
4

3 回答 3

2

您需要单独并按顺序更新每一列,如下所示:

UPDATE YourTable
SET Balance2 = Balance1 
WHERE Balance2 IS NULL AND Balance1 IS NOT NULL

UPDATE YourTable
SET Balance3 = Balance2
WHERE Balance3 IS NULL AND Balance2 IS NOT NULL

为了确保您的所有更新都发生,或者您的更新都不发生,您可能希望将所有语句包装在一个事务中。

于 2012-11-05T19:37:21.623 回答
2

解决方案:

update myTable
set balance9 = coalesce(balance9 ,balance8 ,balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance8 = coalesce(balance8 ,balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance7 = coalesce(balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance6 = coalesce(balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance5 = coalesce(balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance4 = coalesce(balance4 ,balance3 ,balance2 ,balance1)
,   balance3 = coalesce(balance3 ,balance2 ,balance1)
,   balance2 = coalesce(balance2 ,balance1)

工作示例:

declare @myTable table 
(
    clientId nchar(1) not null primary key clustered
    , balance1 int null
    , balance2 int null
    , balance3 int null
    , balance4 int null
    , balance5 int null
    , balance6 int null
    , balance7 int null
    , balance8 int null
    , balance9 int null
)

insert into @myTable (clientId, balance3, balance6)
values ('X',8,12)

update @myTable
set balance9 = coalesce(balance9 ,balance8 ,balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance8 = coalesce(balance8 ,balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance7 = coalesce(balance7 ,balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance6 = coalesce(balance6 ,balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance5 = coalesce(balance5 ,balance4 ,balance3 ,balance2 ,balance1)
,   balance4 = coalesce(balance4 ,balance3 ,balance2 ,balance1)
,   balance3 = coalesce(balance3 ,balance2 ,balance1)
,   balance2 = coalesce(balance2 ,balance1)

select * from @myTable
于 2012-11-05T19:42:08.023 回答
0

了解UPDATE语句。

您需要一些如下语句:

UPDATE
    [YourTableName]
SET
    [Balance5] = '3',
    [Balance6] = '3',
    [Balance7] = '3',
    [Balance8] = '3',
    [Balance9] = '3'
WHERE
    [ClientID] = 'A'

确保您有一个适当的WHERE子句,否则您可能会更新比预期更多的数据。将更新查询包装在TRAN您最初回滚的文件中可能是一个很好的做法,以确保您获得预期的行数。

于 2012-11-05T19:35:49.787 回答