Sql server Query获取表更改结果

时间:2012-10-25 17:04:11

标签: sql-server-2008

我有一张表格如下

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       

我希望看到结果为beblow。

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

请建议!

1 个答案:

答案 0 :(得分: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 with Demo