Reputation: 25
So I have a tbl_total like this.
Name | Total | Month
=========================
David | 87 | Jan
Mike | 67 | Jan
David | 90 | Feb
Mike | 100 | Feb
And I want to display it like this. Could please someone tell me how to do this coz i've no idea at all how to display like this in PHP.
Name | Jan | Feb
===================
David | 87 | 90
Mike | 67 | 100
Upvotes: 2
Views: 70
Reputation: 204756
select name,
sum(case when month = 'Jan' then total else 0 end) as Jan,
sum(case when month = 'Feb' then total else 0 end) as Feb,
sum(case when month = 'Mar' then total else 0 end) as Mar,
sum(case when month = 'Apr' then total else 0 end) as Apr,
sum(case when month = 'May' then total else 0 end) as May,
sum(case when month = 'Jun' then total else 0 end) as Jun,
sum(case when month = 'Jul' then total else 0 end) as Jul,
sum(case when month = 'Aug' then total else 0 end) as Aug,
sum(case when month = 'Sep' then total else 0 end) as Sep,
sum(case when month = 'Oct' then total else 0 end) as Oct,
sum(case when month = 'Nov' then total else 0 end) as Nov,
sum(case when month = 'Dec' then total else 0 end) as `Dec`
from your_table
group by name
Upvotes: 7
Reputation: 154
Try This..
Select Name,SUM(Total),
SUM(case when Month = 'Jan' then total else 0 end ) as Jan,
SUM(case when Month = 'Feb' then total else 0 end ) as Feb
from tbl_total
group by Name
Repeat the Case
statement with different Months as required.
Upvotes: 0