This topic is locked

Using SQL to shape chart data

11/11/2012 1:06:45 PM
PHPRunner General questions
G
gudon author

Hi,
I have read the manual at here, but still could not resolve my problem.


My original table - monthly_subtotal has four columns: ID, Grant,Subtotal,Month. Its structure and data are shown on left in the picture. From this table, I need to make a chart like the one shown above (bottom-right). To achieve this, my idea is to make a new table from the original one, and by using SQL query, re-shape the table structure so that it looks like the one on top-right. The desired new table keeps column Month (distinct) but will use all the data values of the Grant column (distinct) in the original table as column names. These new columns take the corresponding data values of column Subtotal as their data.
Could anybody help build the SQL query? Thanks so much.

Sergey Kornilov admin 11/12/2012

Unfortunately you cannot do that via a "normal" SQL Query. Some databases do provide such functionality though. In MS Access, for instance, you can use TRANSFORM query.