I have two tables that each have date and amount fields. This is for a donation system so one is pledges and the other is donations. Here are the structure of relevant tables and fields:
pledges
- pledge_date
- amount_pledged
donations
- processed_date
- amount_donated
I would like to end up with results that sum pledges and donations by month. Resulting in:
Year Month Pledged Donated
2015 01 120 80
2015 02 90 100
2015 05 0 10
2015 06 120 0
Obviously it's easy enough to get the data summed and grouped from the tables individually, but is there a way to get it all in one result set? Here is the sample SQL to get the data just from the pledges table for example:
SELECT year(pledge_date) as year, month(pledge_date) as month, sum(amount_pledged) as pledged
FROM pledges
GROUP by year(pledge_date), month(pledge_date)
ORDER by pledge_date ASC
I am trying to accomplish this in MySQL.
Thanks to anyone for your help or clues on how to structure this!
JOIN
. But you need some column(s) that tie the two tables together.