-1

我有一张如下表

ClientID        AccountNumber   BalanceOnDay0       BalanceOnDay1   BalanceOnDay2       BalanceOnDay3       BalanceOnDay4       BalanceOnDay5       BalanceOnDay6       BalanceOnDay7
ABC1            123             10                  NULL            NULL                NULL                NULL                NULL                NULL                NULL    
ABC1            123             NULL                NULL            NULL                NULL                NULL                NULL                NULL                3       

我希望看到如下结果。

ClientID        AccountNumber   BalanceOnDay0       BalanceOnDay1   BalanceOnDay2       BalanceOnDay3       BalanceOnDay4       BalanceOnDay5       BalanceOnDay6       BalanceOnDay7
ABC1            123             10                  NULL            NULL                NULL                NULL                NULL                NULL                3

请建议!

4

1 回答 1

2

SUM()如果您想组合余额值,如果您有多个记录,则可以使用:

select clientid,
  accountnumber,
  sum(BalanceOnDay0) BalanceOnDay0, 
  sum(BalanceOnDay1) BalanceOnDay1, 
  sum(BalanceOnDay2) BalanceOnDay2,
  sum(BalanceOnDay3) BalanceOnDay3,
  sum(BalanceOnDay4) BalanceOnDay4, 
  sum(BalanceOnDay5) BalanceOnDay5,
  sum(BalanceOnDay6) BalanceOnDay6, 
  sum(BalanceOnDay7) BalanceOnDay7
from table1
group by clientid, accountnumber

请参阅带有演示的 SQL Fiddle

于 2012-10-25T17:09:04.850 回答