Want to show all my expenses monthly and group yearly like the picture bellow [][2] and the code looks like this
SELECT
monthname(date_added) as monthname,
year(date_added) as year,
SUM(amount) as amounts
FROM incomes GROUP BY year ORDER BY monthname
2017
Jan $100
Feb $200
Jul $90
Jun $300
seem you need order by date and month
select t.year, t.monthname, t.amounts
from(
SELECT monthname(date_added) as monthname,
month(date_added) month,
year(date_added) as year,
SUM(amount) as amounts
FROM incomes
GROUP BY year, month, monthname
)
ORDER BY t.year, t.month
or if you need two separated result for this year and the previous year
for actual year you could
select t.year, t.monthname, t.amounts
from(
SELECT monthname(date_added) as monthname,
month(date_added) month,
year(date_added) as year,
SUM(amount) as amounts
FROM incomes
where year(date_added) = year(curdate())
GROUP BY year, month, monthname
)
ORDER BY t.year, t.month
and for the previous year you could
select t.year, t.monthname, t.amounts
from(
SELECT monthname(date_added) as monthname,
month(date_added) month,
year(date_added) as year,
SUM(amount) as amounts
FROM incomes
where year(date_added) = year(curdate()) -1
GROUP BY year, month, monthname
)
ORDER BY t.year, t.month