Hello i am trying to figure out how to do this i have a mysql table
| ID | ACC_ID | line_id | code |
| 1 | 1 | 5960 | DCA |
| 2 | 1 | 5960 | AAA |
| 3 | 1 | 5960 | DDD |
| 4 | 1 | 5960 | DER |
| 5 | 1 | 5054 | DCB |
| 6 | 1 | 5054 | AAC |
| 7 | 1 | 5011 | DDE |
| 8 | 1 | 5012 | DEQ |
etc the database goes down about 10000 lines
I would like to make a mysql select statement that will do this
| ACC_ID | line_id | code | code | code | code |
| 1 | 5960 | DCA | AAA | DDD | DER |
| 1 | 5054 | DCB | DER | | |
| 1 | 5011 | DDE | | | |
| 1 | 5012 | DEQ | | | |
there could be up to ten codes per line_id
Now my question is it possible to make the query above using a select statement.
Thank you all for your help
This is a
PIVOTbut MySQL does not have aPIVOTfunction but you can replicate it with an aggregate function and aCASEstatement. MySQL also does not have the easiest ways to assign row number by group, but the following is a sample of how you could achieve this using SQL. Since you said you can have up to 10 codes perline_idI hard-coded a possible solution.:See SQL Fiddle with Demo
Result: