Grouping values with IF, month-by-month, in a select statement
<p>I am trying to run a report that will group by month the totals of expense accounts from an expense table which has the followings columns on each row:</p>

<pre><code>expense_acc
debit
credit
post_date
</code></pre>

<p>The desired output of the report is in the following column format:
EXP ACC - JAN - FEB - MAR   </p>

<p>This is my SQL select query:</p>

<pre><code>SELECT expense_acc,
if(MONTH(post_date)=1,SUM(expenses.debit-expenses.credit),0) AS 'JAN',
if(MONTH(post_date)=2,SUM(debit-credit),0) AS 'FEB',
if(MONTH(post_date)=3,SUM(debit-credit),0) AS 'MAR'
FROM expenses
WHERE YEAR(expenses.entered)='2016'
GROUP BY expenses.expense_acc
</code></pre>

<p>The results are not grouping the expense values by month as expected. I am seeing grouping in the first row, regardless of the transaction date.</p>