views:

56

answers:

2

I have a table that looks like this:

|date      |category_id|val
|2010-08-09|1          |2
|2010-08-09|2          |45
|2010-08-10|3          |1500
|2010-08-10|2          |4

I would like to select from this table, that each category id is a column so the end result looks like this:

|date      |1    |2   |3 
|2010-08-09|2    |45  |NULL
|2010-08-10|NULL |4   |1500

Is something like this possible with a single SELECT statement or stored procedure, without using an extra table and without knowing all category_id values beforehand?

A: 

Here is a possible solution.

http://www.futhark.ch/mysql/106.html

codingguy3000
+1  A: 

I think you want to use MYSql's sign to do this. Here is a good link that does something very similar to what you are tryign to do:

http://en.wikibooks.org/wiki/MySQL/Pivot_table

Abe Miessler
While I still need to know what values I have in the category_id column, this solution is easy enough to generate the SQL SELECT with a small script.
chiborg