Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take sum of column with same id in SQL? [duplicate]

Possible Duplicate:
sql query to sum the data

I have following table structure

TradeId     TableName        PricingSecurityID  Quantity    Price   
2008        Fireball.dbo.Bond    506             50         100.0000    
2009        Fireball.dbo.Bond    506             50         100.2500    
2010        Fireball.dbo.Bond    588             50         100.7500    
2338        Fireball.dbo.Bond    588             100        102.5000    

I need to take a sum of Quantity of matching or we can say group by particular PricingSecurityID

like for PricingSecurityID=506 I should get quantity=100

and for PricingSecurityID=588 I should get quantity=150

How can I write this SQL query?

I did try with simple group by statement but as i'm also selecting tradeid i'm getting error : Column 'TradeId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

like image 333
Neo Avatar asked Oct 12 '12 17:10

Neo


2 Answers

SELECT PricingSecurityID, SUM(ISNULL(Quantity,0))
  FROM Table
 GROUP BY PricingSecurityId;
like image 98
Kumar_2002 Avatar answered Oct 20 '22 09:10

Kumar_2002


select PricingSecurityID, sum(quantity)
from Fireball.dbo.Bond
group by PricingSecurityID
like image 26
Johanna Larsson Avatar answered Oct 20 '22 08:10

Johanna Larsson