I have two tables that I need to join and get SUM() of one column in each of the table.
mysql> select * from day_sales2;
+--------+------------+
| id | ds_oversht |
+--------+------------+
| 119263 | -0.17 |
| 119336 | 0.55 |
| 119409 | 3.08 |
| 119482 | -33.25 |
| 119555 | 1.27 |
| 119628 | 0.32 |
| 119701 | -0.15 |
| 119774 | 6.57 |
| 119847 | -0.06 |
| 119920 | -12.04 |
| 119993 | -12.40 |
| 120066 | -38.90 |
| 120139 | 20.56 |
| 120212 | -17.11 |
| 120285 | -0.51 |
| 120358 | 0.04 |
| 120431 | 3.23 |
| 120504 | -0.89 |
| 120577 | 2.13 |
| 120650 | 0.57 |
| 120723 | -1.15 |
| 120796 | 1.99 |
| 120869 | 9.29 |
| 120942 | 6.95 |
| 121015 | 3.22 |
| 121088 | -0.59 |
| 121161 | -0.89 |
| 121234 | -1.00 |
+--------+------------+
mysql> select * from over_short_adj2;
+-------+---------+-----------+
| id | main_id | cd_amount |
+-------+---------+-----------+
| 45881 | 119920 | 0.66 |
| 45882 | 119920 | 1.19 |
| 45907 | 119920 | 4.81 |
| 46017 | 120212 | 3.35 |
| 46018 | 120066 | 16.85 |
+-------+---------+-----------+
I'm using this Query to get that done:
SELECT SUM(t1.ds_oversht) sum1, SUM(t2.cd_amount) sum2
FROM day_sales2 t1
LEFT JOIN over_short_adj2 t2 ON t1.id = t2.main_id
But the sum1 is incorrect because join is one to many. How could I get this query work?
sum1is incorrect?