2016-09-09 22 views
0

誰かが私に教えてくれますか、会計年度内に金額を表示する方法を理解できますか?私が使用している会計年度は11月から10月です。ですから、2016年の会計年度は2015年11月から2016年10月までです。MySQL - 会計年度内に毎月の金額の合計を取得する

私の問題は、選択した年の月間の金額のみを表示できることです。どうすれば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 

私はまた、このような出力を必要としています:

enter image description here

は、あなたの答えや提案に感謝! :)

+0

を試してみてください - それは我々がアプリケーションレベルまたはプレゼンテーションleyerでそれを行うことはできません – Strawberry

+0

利用可能だと仮定します。 – Dhenn

+0

その場合はhttp://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simpleを参照してください。 -sql-query – Strawberry

答えて

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 
+0

申し訳ありませんが、それは私が探しているものではありません。行ではなく列として扱われる月が必要です。とにかくありがとう。そして、私の入力は2016年の1年に過ぎないはずです – Dhenn

関連する問題