MySQL - 获取一个会计年度内每月的金额总和

时间:2016-09-09 09:54:39

标签: mysql

有人可以教我或帮我弄清楚如何在一个财政年度内显示金额吗?我使用的会计年度是11月到10月。因此,请说2016财年是2015年11月至2016年10月。

现在我的问题是我只能在所选年份内显示每月的金额。我怎么能在mysql中做到这一点?

This is my table:

这是我尝试的查询,但它只在一年内有效。

  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

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

我也需要这种输出:

enter image description here

感谢您的回答或建议! :)

1 个答案:

答案 0 :(得分:0)

试试这个

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