Bolster
Bolster

Reputation: 7916

MySQL Sum one field based on a second 'common' field

Not a SQL guy, so i'm in a bind, so I'll keep this simple

+--------------+------------------------+
| RepaymentDay | MonthlyRepaymentAmount |
+--------------+------------------------+
|            3 |               0.214847 | 
|           26 |               0.219357 | 
|           24 |               0.224337 | 
|            5 |               0.224337 | 
|           18 |               0.224337 | 
|           28 |               0.214847 | 
|            1 |               0.224337 | 
|           28 |               0.327079 |
+--------------+------------------------+

I am looking for a query that would then give something like;

+--------------+------------------------+
| RepaymentDay | MonthlyRepaymentAmount |
+--------------+------------------------+
|            1 |               0.224337 | 
|            3 |               0.214847 | 
|            5 |               0.224337 | 
|           18 |               0.224337 | 
|           24 |               0.224337 | 
|           26 |               0.219357 | 
|           28 |               0.541926 | 
+--------------+------------------------+

I.e Where there are multiple records with the same value of 'RepaymentDay' , to sum those values of 'MonthlyRepaymentAmount'.

I could do it externally with perl or python, no issue, but I want to try and become more familiar with SQL. Any ideas?

Upvotes: 2

Views: 271

Answers (2)

Jeremy
Jeremy

Reputation: 122

select RepaymentDay, sum(MonthlyRepaymentAmount) from table_name group by RepaymentDay;

Upvotes: 1

Martin Smith
Martin Smith

Reputation: 453707

SELECT RepaymentDay, SUM(MonthlyRepaymentAmount) AS MonthlyRepaymentAmount
FROM YourTable
GROUP BY RepaymentDay
ORDER BY RepaymentDay

Upvotes: 6

Related Questions