根据Colimn中的某些数据添加值

时间:2016-05-23 11:44:38

标签: sql

我无法在网上找到任何内容

我有3列如此:

 Customer       Currency         Balance
|  a     |      |   AUD |       |  22.5    |      
|  b     |      |   GBP |       |  30.0    |      
|  c     |      |   GBP |       |  45.5    |      
|  d     |      |   USD |       |  56.9    |      
|  e     |      |   USD |       |  45.4    |      
|  f     |      |   EUR |       |  28.0    |      

我希望获得每种货币的全部总和,例如:所以所有余额的当前为GBP,将它们加在一起等等

我是SQL的新手,再一次,我找不到任何在线的东西

2 个答案:

答案 0 :(得分:3)

SELECT Currency, SUM(Balance) AS total
FROM currencies
GROUP BY Currency

答案 1 :(得分:0)

使用以下代码,这适用于SQL Server 2012

DECLARE @table TABLE (Customer Varchar(20),Currency varchar(20),Balance float)
INSERT @table
(Customer,Currency,Balance)
VALUES
('a','AUD',22.5),    
('b','GBP',30.0),    
('c','GBP',45.5),    
('d','USD',56.9),    
('e','USD',45.4),    
('f','EUR',28.0)

SELECT 
    Currency, Sum(Balance) AS Total
FROM 
    @table
GROUP BY 
    Currency