Hi I have 2 tables and I want to group by month
+-----------+----------+-------+
| Parent ID | Payments | Month |
+-----------+----------+-------+
| 1 | 100 | 1 |
| 2 | 120 | 1 |
| 3 | 130 | 2 |
+-----------+----------+-------+
+----------+------------+----------+
| Child ID | Parent Ref | Sold |
+----------+------------+----------+
| 1 | 1 | 20 |
| 2 | 1 | 30 |
| 3 | 2 | 50 |
| 4 | 2 | 10 |
+----------+------------+----------+
Expected Output should be
+----------+------+-------+------------------------------------------+
| Payments | Sold | Month | Notes (no need sql) |
+----------+------+-------+------------------------------------------+
| 220 | 110 | 1 | <-220=sum(100+120), sum(110=20+30+50+10) |
| 130 | 0 | 2 | <-130=130, null or 0 doesnt matter |
+----------+------+-------+------------------------------------------+
What I'm getting with my query i think is the parent is multiplying its sum. I'm using a live database so not really sure if its multiplying by the number of child, but its multiplying somewhere. All the child sum result are ok, all the parents arnt. I've compared this against my previous SQL to make sure. The reason why im not using the old sql is its extremely slow due to many db calls and php processing.
+----------+------+-------+---------------------------------------------------------------------+
| Payments | Sold | Month | Notes (no need sql) |
+----------+------+-------+---------------------------------------------------------------------+
| 880 | 110 | 1 | <-220=sum(100+120)*4 as there are 4 childrows, sum(110=20+30+50+10) |
| 130 | 0 | 2 | <-130=130, null or 0 doesnt matter |
+----------+------+-------+---------------------------------------------------------------------+
My Query
Select sum(parent.Payments), sum(child.Sold)
from parent, child
where
parent.id = child.parent_ref group by parent.month