简体   繁体   中英

MySQL - get the sum of amount every month within a fiscal year

can someone teach me or help me to figure out how to display amount within a fiscal year?. The fiscal year I am using is November to October. So let say Fiscal Year 2016 is from November 2015 to October 2016.

Now my problem is I can only display the amount per month within the chosen year. How can I do this in mysql?

这是我的桌子:

This is the query I have tried, but it works only within a year.

  select a.account, a.region, sum(n.amount) as 'Total Net', sum(case when n.savings_date between date_format(n.savings_date, '%Y-11-01') and last_day(date_format(savings_date, '%Y-11-01')) then n.amount end) as `Nov`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-12-01') and last_day(date_format(savings_date, '%Y-12-01')) then n.amount end) as `Dec`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-01-01') and last_day(date_format(savings_date, '%Y-01-01')) then n.amount end) as `Jan`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-02-01') and last_day(date_format(savings_date, '%Y-02-01')) then n.amount end) as `Feb`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-03-01') and last_day(date_format(savings_date, '%Y-03-01')) then n.amount end) as `Mar`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-04-01') and last_day(date_format(savings_date, '%Y-04-01')) then n.amount end) as `Apr`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-05-01') and last_day(date_format(savings_date, '%Y-05-01')) then n.amount end) as `May`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-06-01') and last_day(date_format(savings_date, '%Y-06-01')) then n.amount end) as `Jun`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-07-01') and last_day(date_format(savings_date, '%Y-07-01')) then n.amount end) as `Jul`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-08-01') and last_day(date_format(savings_date, '%Y-08-01')) then n.amount end) as `Aug`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-09-01') and last_day(date_format(savings_date, '%Y-09-01')) then n.amount end) as `Sep`, sum(case when n.savings_date between date_format(n.savings_date, '%Y-10-01') and last_day(date_format(savings_date, '%Y-10-01')) then n.amount end) as `Oct` from net_savings n left join accounts a on a.id = n.account_id where year(n.savings_date) = '2016' group by a.account order by a.account desc 

I am needing this kind of output also:

在此处输入图片说明

Appreciate your answers or suggestions! :)

Try this

SELECT
    a.account,
    a.region,
    YEAR(n.savings_date),
    MONTH(n.savings_date),
    SUM(n.amount)
FROM
    net_savings n
LEFT JOIN
    accounts a
ON
    a.id = n.account_id
WHERE
    n.savings_date >= '2015-11-01' and n.savings_date < '2016-09-01'
GROUP BY
    a.account,
    a.region,
    YEAR(n.savings_date),
    MONTH(n.savings_date),
ORDER BY
    a.account DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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