0

I want to achieve output using mysql query as described in the screen shot.

enter image description here

in the screen shot, its showing amounts for last 12 months.

if I am running it in Jan 2016, it should show column names from Feb-15 to Jan-16.

Any ideas, how can this be achieved?

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
Muhammad
  • 611
  • 7
  • 14
  • 32
  • 2
    you need to post your source data structure and sample data. and explain the second column which is always 0.00 and doesnt have a header – amdixon Dec 26 '15 at 23:57
  • 1
    Possible duplicate of [MySQL pivot table query with dynamic columns](http://stackoverflow.com/questions/12598120/mysql-pivot-table-query-with-dynamic-columns) – Shadow Dec 26 '15 at 23:59

1 Answers1

0
CREATE TABLE Months (
    mo INT );
INSERT INTO Months VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11);

SELECT DATE_FORMAT(DATE(CONCAT(LEFT(NOW(), 7), '-15')
                        - INTERVAL mo MONTH),
                   "%b-%y") AS MmmYY
    FROM Months ORDER BY mo DESC;

That should give you the headings you want. Then to do the pivoting, see http://mysql.rjweb.org/doc.php/pivot

Rick James
  • 135,179
  • 13
  • 127
  • 222