2016-05-23 72 views
1

我一直無法在網絡上找到任何增加值的Colimn

我有一個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 |  

我想每一個充分總結貨幣,例如:使所有其currecy是GBP餘額,在

林在一起,所以將它們添加到新的SQL又一次,我不能在網上找到的任何東西,這是否

+5

提示:'GROUP BY'。 –

回答

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

只有當任何人擊敗@戈登才能回答的時候,他不想回答:-) –

+0

嗯,我眨了眨眼睛,突然之間,有一個答案!我不能接受另外6分鐘,但我會 感謝您的倉促回覆 – Nonagon

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