I'm performing a query in MySQL like so...
select employee_id, sale_date, count(id)
from sales
group by employee_id, extract(year_month from sale_date);
The data comes back like...
1 1/1/2014 5
1 2/1/2014 6
1 3/1/2014 4
2 1/1/2014 4
2 2/1/2014 10
2 3/1/2014 0
3 1/1/2014 0
3 2/1/2014 0
3 3/1/2014 8
I pivot this table and what I want to do is display in the following format
ID Jan Feb Mar
1 5 6 4
2 4 10 0
3 0 0 8
In the this table, what I want is column names that correspond to the month. If you're in the middle of the year though, the I want the 1st column to be whatever month you're in, not Jan.
As to my question - is it possible anyhow to set the name of the column dynamically within the same SQL statement? Since I have it pivoted, I can extract the month name, I'm just missing a step of actually writing the column name.