简体   繁体   English

Mysql左加入和总和不能按月和年正确地与组

[英]Mysql Left Join And Sum not Working Correctly with group by month and year

I want to get a sum of total_amount and received_amt with group by with month and year on mysql . 我想在mysql上获得与group by的月份和年份的total_amount和Received_amt的总和。 But I am facing a problem. 但是我面临一个问题。 The problem is second table column sum not correct with group by month and year. 问题是第二个表的列总和与按月和年分组不正确。

Current Query 当前查询

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

Example: 例:

First Table: new_order 第一个表:new_order

Second Table: sell_master 第二张表:sell_master

Table Structure: 表结构:

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_master

+----------------------------------------+
|sell_date(date)  | total_amount(double) |
+----------------------------------------+
|2007-10-06       | 10                   |
+----------------------------------------+

Current Result 当前结果

+---------------------------------------+
|order_date | SumOfNO | SumOfSM | Total |
+---------------------------------------+
|2007-10-06 | 1130    | 40      |1170   |
+---------------------------------------+

Expected Result 预期结果

+---------------------------------------+
|order_date | SumOfNO | SumOfSM | Total |
+---------------------------------------+
|2007-10-06 | 1130    | 10      |1140   |
+---------------------------------------+

You shouldn't use SUM(t2.total_amount) in the main query. 您不应在主查询中使用SUM(t2.total_amount) You already calculated the sum in the subquery, you should use that. 您已经在子查询中计算了总和,应该使用该总和。 What's happening is that you're multiplying t2.total_amount by the number of rows in new_order that matches. 发生的事情是您将t2.total_amount乘以new_order中匹配的行数。

There's also no need to use IFNULL() inside SUM() , since SUM() ignores null values (most aggregation functions do). 也不需要在SUM()使用IFNULL() ,因为SUM()忽略空值(大多数聚合函数都这样做)。

The subquery should select the year and month of the date, so you can join on those directory, rather than using date_format . 子查询应选择日期的年和月,因此您可以加入这些目录,而不必使用date_format

And since you're grouping by month, you shouldn't select t1.order_date -- that will just pick a random day of the month from the group. 而且由于您是按月分组,因此您不应该选择t1.order_date ,它只会从该组中随机选择一个月中的某天。 You should just show the month in YYYY-MM format. 您应该只以YYYY-MM格式显示月份。

SELECT 
    DATE_FORMAT(t1.order_date, '%Y-%m') AS month,
    sum(t1.received_amt) as SumOfNO,
    IFNULL(t2.Amount, 0) as SumOfSM,
    sum(t1.received_amt) + IFNULL(t2.Amount, 0) AS Total
FROM `new_order` t1
LEFT JOIN
 (  select YEAR(t2.sell_date) AS year, MONTH(t2.sell_date) AS month,  sum(total_amount) as Amount
    from sell_master t2
    group by year, month
 ) t2
 ON YEAR(t1.order_date) = t2.year AND MONTH(t1.order_date) = month
GROUP BY month
ORDER BY month DESC

DEMO DEMO

You want to select an aggregate results (sums here) of one table combined with a aggregate results of another. 您想要选择一个表的汇总结果(此处为总和)和另一表的汇总结果。 So build both aggregates, then join. 因此,建立两个聚合,然后加入。

select 
  year_month, 
  o.total as sumofno,
  coalesce(s.total, 0) as sumofsm,
  o.total + coalesce(s.total, 0) as total
from
(
  select format(order_date, 'yyyy-MM') as year_month, sum(received_amt) as total
  from new_order
  group by format(order_date, 'yyyy-MM')
) o
left join
(
  select format(sell_date, 'yyyy-MM') as year_month, sum(total_amount) as total
  from sell_master
  group by format(sell_date, 'yyyy-MM')
) s using (year_month)
order by month desc;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM