0

我需要查询下表

桌子

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    

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

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
4

2 回答 2

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
于 2012-11-05T18:11:12.023 回答
0

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

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 
于 2012-11-05T18:24:47.417 回答