SQL Server 2008查询以填充空值

时间:2012-11-05 18:07:10

标签: sql-server-2008

我需要查询下表

ClienName   Bal0    Bal1    Bal2        Bal3        Bal4        Bal5        Bal6    
--------------------------------------------------------------------------------
A           600     600     NULL        NULL        NULL        NULL        NULL    
B           Null    100     NULL        NULL        NULL        NULL        NULL    
C           NULL    NULL    200          NULL       NULL        NULL        NULL        
D           NULL    NULL    NULL        NULL        NULL        130         130     
E           510     510     NULL        510         510         NULL        NULL    
F           170     170     NULL        170         170         NULL        170 
G           210     210     NULL        210         210         210         210     
H           20      20      NULL        20          NULL        NULL        NULL    

结果:我想看看上面的TABLE如下,哪个查询可以做到?请帮助

ClienName   Bal0    Bal1    Bal2        Bal3        Bal4        Bal5        Bal6    
--------------------------------------------------------------------------------
A           600     600     600         600         600         600         600     
B           100     100     100         100         100         100         100     
C           200     200     200         200         200         200         200     
D           130     130     130         130         130         130         130         
E           510     510     510         510         510         510         510     
F           170     170     170         170         170         170         170 
G           210     210     210         210         210         210         210     
H           20      20      20          20          20          20          20

2 个答案:

答案 0 :(得分:3)

假设我从你非常有限的示例集中理解了你想要的东西,只需逐一填表并逐列填充。

向前一个方向:

UPDATE theTable
SET Bal1 = Bal0
WHERE Bal1 IS NULL AND Bal0 IS NOT NULL;

UPDATE theTable
SET Bal2 = Bal1
WHERE Bal2 IS NULL AND Bal1 IS NOT NULL;

...

反过来一次:

UPDATE theTable
SET Bal5 = Bal6
WHERE Bal5 IS NULL AND Bal6 IS NOT NULL;

UPDATE theTable
SET Bal4 = Bal5
WHERE Bal4 IS NULL AND Bal5 IS NOT NULL;

...

这也(假设“Bal”表示平衡,数字与时间有关)随着时间的增加处理列中不同值的情况。


如果您只想要第一个非空列,请尝试使用COALESCE运算符:

SELECT ClienName, COALESCE(Bal0, Bal1, Bal2, Bal3, Bal4, Bal5, Bal6) AS firstBal
FROM theTable

答案 1 :(得分:0)

如果您只想查询max(假设每行只有一个值)并且不更新现有表,那么您可以这样做

declare @table table(clientname varchar(5), bal0 int, 
                    bal1 int, bal2 int, bal3 int, bal4 int, bal5 int, bal6 int)

insert into @table 
select 'A', 600, 600, null, 600, 600, null, null
UNION ALL
select 'B', null, 120, null, null, 120, null, null

;with cte as (
select clientname, (SELECT MAX(val) 
                    FROM (VALUES (bal0),(bal1),(bal2),(bal3),
                          (bal4),(bal5),(bal6)) AS value(val)) As mCOL
from @table 
)

select t.clientname, mCOL as bal0, mCOL as bal1, mCOL as bal2, mCOL as bal3, 
mCOL as bal4, mCOL as bal5, mCOL as bal6
from @table t inner join cte c on t.clientname = c.clientname