Search code examples
sqlsummary

How to Sum multiple lines in sql


I have multiple lines of data all sharing the same Company id.

Is there a way to 'sum' all the amounts to give me one line of data per company id using SQL Server Management Studio 2005?

For example I currently have the below data......

Company_Name   Company_ID   Amount
Company 6         10024   120
Company 6         10024   569
Company 6         10024    53
Company 6         10024   100
Company 6         10024   564
Company 7         10638  9500
Company 7         10638   105
Company 7         10638   624

What i would like to try and get is.......

Company_ Name   Company_ID     Amount 
Company 6        10024         1406
Company 7        10638        10229

Is there a way of doing this?

Any advice pointing me the right way would be great.

Thanks,


Solution

  • SELECT Company_Name, Company_ID, SUM(Amount) 
    FROM TableName GROUP BY Company_Name, Company_ID