2014-01-21 81 views
0

我試圖獲得將所有興趣列作爲最終數字加起來的最終列,以便每個客戶有一個數字。我目前有:如何對客戶分組的多列和多行進行求和

+--------+--------+-------+-------+-------+-------+ 
|Customer|Number |IntMax |IntMin |IntMin1|IntMax1| 
+--------+--------+-------+-------+-------+-------+ 
|Jones |72352516|$0.00 |$381.47|$0.00 |$0.00 | 
+--------+--------+-------+-------+-------+-------+ 
|Jones |72352516|$455.31|$0.00 |$0.00 |$0.00 | 
+--------+--------+-------+-------+-------+-------+ 
|Brett |70920356|$0.00 |$0.00 |$194.56|$129.71| 
+--------+--------+-------+-------+-------+-------+ 
|Gavin |79023561|$0.00 |$617.29|$0.00 |$0.00 | 
+--------+--------+-------+-------+-------+-------+ 
|Gavin |79023561|$531.46|$0.00 |$0.00 |$0.00 | 
+--------+--------+-------+-------+-------+-------+ 

我希望看到的結果是:

+--------+--------+---------+ 
|Customer|Number |IntFinal | 
+--------+--------+---------+ 
|Jones |72352516|$836.78 | 
+--------+--------+---------+ 
|Brett |70920356|$324.27 | 
+--------+--------+---------+ 
|Gavin |79023561|$1,148.76| 
+--------+--------+---------+ 

非常感謝

回答

0
SELECT Customer, Number, 
     SUM(IntMax + IntMin + IntMin1 + IntMax1) AS IntFinal 
    FROM Table1 
GROUP BY Customer, Number 

輸出:

 
| CUSTOMER | NUMBER | INTFINAL | 
|----------|----------|----------| 
| Brett | 70920356 | 324.27 | 
| Jones | 72352516 | 836.78 | 
| Gavin | 79023561 | 1148.75 | 

這裏是SQLFiddle演示

0
<!-- language: lang-sql --> 
    SELECT Customer,Number,SUM((IntMax+IntMin+IntMin1+IntMax1)) as IntFinal 
    FROM tablename 
GROUP BY Customer,Number 
0
create table #temp 
(
    Customer varchar(12) not null, 
    Number int not null, 
    IntMax decimal(19,6), 
    IntMin decimal(19,6), 
    IntMin1 decimal(19,6), 
    IntMax1 decimal(19,6), 
) 
insert into #temp values('Jones',72352516,'0.00','381.47','0.00','0.00') 
insert into #temp values('Jones',72352516,'455.31','0.00','0.00','0.00') 
insert into #temp values('Brett',70920356,'0.00','0.00','194.56','129.71') 
insert into #temp values('Gavin',79023561,'0.00','617.29','0.00','0.00') 
insert into #temp values('Gavin',79023561,'531.46','0.00','0.00','0.00') 

select * from #temp 

select t1.Customer, t1.Number, SUM(T1.IntMax+t1.IntMax1+t1.IntMin+t1.IntMin1) as IntFinal from #temp t1 group by t1.Customer, t1.Number 

drop table #temp 
0

感謝您的所有答案的傢伙 - 我最終找到了答案下面也:

SELECT Interest_Min_Max1.Customer,CCur函數(總和([IntMax])+總和([ IntMin])+ Sum([IntMax1])+ Sum([IntMin1]))AS總計 FROM表1 GROUP BY Interest_Min_Max1.Customer,Interest_Min_Max1.Number;

相關問題