简体   繁体   English

最近12个月的MySQL总和

[英]Mysql sum for last 12 months

I am trying to create a query to get the last 12 month records based on month for chart representation. 我正在尝试创建一个查询,以获取基于月份的图表表示形式的最近12个月的记录。 After a lot of reading and after watching this similar topic I created a query that seems right but missing the months with 0 money. 经过大量阅读并观看了类似的主题之后,我创建了一个查询,看起来不错,但缺少零钱的月份。 As an example, I see in my graph months 1/14,2/14,4/14 and so on... 3/14 is missing. 例如,我在图表中看到1 / 14,2 / 14,4 / 14,依此类推... 3/14丢失了。

My code is this 我的代码是这样

SELECT *
FROM
    (SELECT DATE_FORMAT(now(), '%m/%y') AS Month
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 1 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 2 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 3 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 4 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 5 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 6 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 7 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 8 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 9 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 10 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 11 MONTH), '%m/%y')
    ) AS Months
LEFT JOIN
    (SELECT sum(expenses.price) AS ExpenseAmount,
            sum(payments.amount) AS PaymentsAmount,
            DATE_FORMAT(expenses.date_occurred,'%m/%y') AS Month,
            DATE_FORMAT(payments.date_occurred,'%m/%y') AS Montha
     FROM expenses,
          payments
     WHERE payments.user_id= 1
         AND payments.user_id=expenses.user_id
     GROUP BY MONTH(payments.date_occurred),
              YEAR(payments.date_occurred)
     ORDER BY payments.date_occurred ASC ) data ON Months.MONTH = data.Montha
ORDER BY data.Montha;

Any help will be great as this kind of queries are too advanced for me :-) 任何帮助将是巨大的,因为这种查询对我来说太高级了:-)

在此处输入图片说明

As the query looks like it should produce a row for each month, can you check the query output, rather than what your graph is producing? 由于查询看起来每个月都会产生一行,因此您可以检查查询输出,而不是图形产生的结果吗? I suspect you've got an entry for 04/14, but that the value is NULL rather than 0. To correct this, you can change the query to start 我怀疑您输入的是04/14,但是该值为NULL而不是0。要更正此问题,可以将查询更改为start

SELECT Months.Month, 
    COALESCE(data.ExpenseAmount, 0) AS ExpenseAmount, 
    COALESCE(data.PaymentAmount, 0) AS PaymentAmount

COALESCE will give you 0 instead of NULL where there are no rows matching your left join. 如果没有匹配左联接的行,则COALESCE将给您0而不是NULL。

However, there are further problems in your query. 但是,查询中还有其他问题。 You will only get rows if there is an expense and a payment in the same month - check http://sqlfiddle.com/#!2/3f52a8/1 and you'll see the problem if you remove some of the data from one table. 如果您在同一个月中有支出和付款,则只会获得行-检查http://sqlfiddle.com/#!2/3f52a8/1 ,如果您从其中删除了一些数据,则会看到问题表。

Here's a working solution which will give you all months, summing the data from both tables, even if only one is present. 这是一个可行的解决方案,即使您只存在一个表,也可以给您几个月的时间,汇总两个表中的数据。 This works by handling the expenses and payments as separate queries, then joining them together. 这是通过将费用和付款作为单独的查询处理,然后将它们合并在一起而起作用的。

SELECT Months.Month, COALESCE(expensedata.ExpenseAmount, 0) AS ExpenseAmount, COALESCE(paymentdata.PaymentAmount, 0) AS PaymentAmount
FROM
    (SELECT DATE_FORMAT(now(), '%m/%y') AS Month
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 1 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 2 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 3 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 4 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 5 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 6 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 7 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 8 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 9 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 10 MONTH), '%m/%y')
     UNION SELECT DATE_FORMAT(DATE_SUB(now(), INTERVAL 11 MONTH), '%m/%y')
    ) AS Months

LEFT JOIN 
  (SELECT SUM(price) AS ExpenseAmount, DATE_FORMAT(date_occurred,'%m/%y') AS Month
   FROM expenses 
   WHERE user_id = 1
   GROUP BY MONTH(date_occurred), YEAR(date_occurred)) expensedata ON Months.Month = expensedata.Month

LEFT JOIN 
  (SELECT SUM(amount) AS PaymentAmount, DATE_FORMAT(date_occurred,'%m/%y') AS Month
   FROM payments
   WHERE user_id = 1
   GROUP BY MONTH(date_occurred), YEAR(date_occurred)) paymentdata ON Months.Month = paymentdata.Month

ORDER BY Months.Month;

SQL Fiddle showing this working: http://sqlfiddle.com/#!2/3f52a8/5 SQL Fiddle显示此工作方式: http ://sqlfiddle.com/#!2/3f52a8/5

i think your problem is the left join. 我认为您的问题是左加入。 the result of the select statement inside your left join does not contain any results with 0 money ... it looks like your 您左联接内的select语句的结果不包含任何0货币的结果...看起来像您的

where payments.user_id = 1 

is the matter... 这件事...

under the following link you can find an explanation to all types of joins... http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ 在以下链接下,您可以找到所有类型的联接的说明... http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

hope that helps 希望能有所帮助

Replace ORDER BY data.Montha to ORDER BY Months.MONTH ORDER BY data.Montha替换为ORDER BY Months.MONTH

There is no '3/14' in data.Montha. data.Montha中没有“ 3/14”。

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

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