Я хочу получить сумму total_amount и receive_amt с группой по месяцу и году на mysql
.Но я столкнулся с проблемой.Проблема в том, что сумма столбцов во второй таблице неверна для группы по месяцу и году.
Текущий запрос
SELECT t1.order_date,sum(IFNULL(t1.received_amt, 0)) as SumOfNO,
sum(IFNULL(t2.total_amount, 0)) as SumOfSM,
SUM(IFNULL(t1.received_amt, 0) + IFNULL(t2.total_amount, 0)) AS Total
FROM `new_order` t1
LEFT JOIN
( select t2.sell_date,t2.total_amount, sum(total_amount) as Amount
from sell_master t2
group by YEAR(t2.sell_date), MONTH(t2.sell_date)
) t2
ON format(t1.order_date,'yyyy-MM') = format(t2.sell_date,'yyyy-MM')
GROUP BY YEAR(t1.order_date), MONTH(t1.order_date)
ORDER BY t1.order_date DESC
Пример:
Первая таблица:new_order
Вторая таблица: sell_master
Структура таблицы:
new_order
+----------------------------------------+
|order_date(date) | received_amt(double) |
+----------------------------------------+
|2007-10-06 | 245 |
|2007-10-06 | 310 |
|2007-10-06 | 275 |
|2007-10-06 | 300 |
+----------------------------------------+
sell_master
+----------------------------------------+
|sell_date(date) | total_amount(double) |
+----------------------------------------+
|2007-10-06 | 10 |
+----------------------------------------+
Текущий результат
+---------------------------------------+
|order_date | SumOfNO | SumOfSM | Total |
+---------------------------------------+
|2007-10-06 | 1130 | 40 |1170 |
+---------------------------------------+
Ожидаемый результат
+---------------------------------------+
|order_date | SumOfNO | SumOfSM | Total |
+---------------------------------------+
|2007-10-06 | 1130 | 10 |1140 |
+---------------------------------------+