MySQL每个月都会获得同比百分比

时间:2016-01-03 22:23:38

标签: mysql percentage mom

我试图在过去的12个月中获得相同的结果,并且他们的价值与去年相同的月份和百分比差异。

ThisYearMonth | ThisYearValue | LastYearMonth | LastYearDifAsPercent
--------------|---------------|---------------|---------------------
Dec 2015 -----|800 -----------|Dec 2014 ------|50 ------------------
Nov 2015 -----|350 -----------|Nov 2014 ------|-23 -----------------

每个选择都可以,但合并后,我得到

  

[Err] 1242 - 子查询返回超过1行


SELECT
    DATE_FORMAT(`month`, '%b %Y') AS ThisYearMonth,
    no_customer_growth as ThisYearCustGrowth,
    DATE_FORMAT(DATE_SUB(`month`, INTERVAL 1 YEAR),'%b %Y') AS `LastYearMonth`,
    (
        SELECT
        ROUND((t1.no_customer_growth - t2.no_customer_growth) / t2.no_customer_growth * 100, 2)
        FROM cust_evolution AS t1
        INNER JOIN cust_evolution AS t2
        ON DATE_FORMAT(t1.`month`,'%b') = DATE_FORMAT(t2.`month`,'%b')
        AND DATE_FORMAT(t1.`month`,'%Y') = DATE_FORMAT(t2.`month`,'%Y') - 1
    ) AS `MoM % growth`
FROM cust_evolution

感谢您的时间和支持!

0 个答案:

没有答案