Possible Duplicate:
SQL “transform” query
I can’t figure out how to compose a SQL statement to display the different values of a table as separate columns…here’s an example:
Table A - Project
= = = = = = = = = = = = = =
ID | Project
= = = = = = = = = = = = = =
1 | Google
Table B - Tasks
= = = = = = = = = = = = = = = = = = = = = = = = = = = =
ID | Project ID | Task_name | Date Complete
= = = = = = = = = = = = = = = = = = = = = = = = = = = =
1 | 1 | Build | 10/30/12
2 | 1 | Revise | 11/15/12
3 | 1 | Launch | 11/30/12
SQL outputs in the following format:
Website | Build Date | Revise Date | Launch Date
= = = = = = = = = = = = = = = = = = = = = = = = = = = =
Google | 10/30/12 | 11/15/12 | 11/30/12
This is known as a
PIVOTbut MySQL does not have aPIVOTfunction but you can replicate it using an aggregate function and aCASEstatement:See SQL Fiddle with Demo
Result: