Reputation: 11
I'm trying to achieve yearly data as pivot, this is my query
SELECT
month(te.topup_date) as month,
sum(CASE WHEN year(te.topup_date) = '2015' THEN te.topup_amount+ tp.topup_amount END) as '2015',
sum(CASE WHEN year(te.topup_date) = '2016' THEN te.topup_amount+ tp.topup_amount END) as '2016'
from te_daily_topup as te
inner join tp_daily_topup as tp on year(te.topup_date) = year(tp.topup_date)
where year(te.topup_date) between '2015' and '2016'
group by year(te.topup_date), month(te.topup_date)
The result from the above query
month | 2015 | 2016
--------------------
1 | 123 |
2 | 2343 |
1 | |234
2 | |7667
The result I'm looking for is below:
month | 2015 | 2016
--------------------
1 | 123 | 234
2 | 2343 | 7667
help me to modify this query..
Upvotes: 0
Views: 28
Reputation: 1269653
Just remove year()
from the GROUP BY
:
select month(te.topup_date) as month,
sum(case when year(te.topup_date) = 2015 then te.topup_amount + tp.topup_amount end) as `2015`,
sum(case when year(te.topup_date) = 2016 then te.topup_amount + tp.topup_amount end) as `2016`
from te_daily_topup te inner join
tp_daily_topup tp
on year(te.topup_date) = year(tp.topup_date)
where year(te.topup_date) between 2015 and 2016
group by month(te.topup_date);
Note: Do not use single quotes for integer constants nor for column aliases. Only use single quotes for string and date constants.
Upvotes: 1