user3607758
user3607758

Reputation: 123

SQL calculating sum based on another column

I have a table with the following data in it:

Account number     Amount
13                 40
34                 30
14                 30
13                 60
14                 10

I would like to know how I can write a query to return the following results

Account number     Total amount
13                 100
14                 40
34                 30

The query should calculate the sum of all of the amounts in the amount column that share the same account number.

Any help would be much appreciated!

Upvotes: 2

Views: 4227

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460278

Use Group By + SUM

SELECT [Account number],
       SUM(Amount) As [Total Amount]
FROM dbo.Table1
GROUP BY [Account Number]
ORDER BY SUM(Amount) DESC

Demo

Upvotes: 2

TechDo
TechDo

Reputation: 18659

Please try:

select
    [Account Number],
    sum(Amount) Amount
from
    YourTable
Group by [Account Number]

Upvotes: 0

Related Questions