i need to create 2 additional columns from data in another column in the same table. i have a columns called Role, specifying different roles, e.g. Supervisor, Administrator, etc.
Now i want to create a column for Supervisor and a column for Administrator, where the e.g. ClientName is the same in example attached
expected result
ClientName Role Name
A123 Supervisor Dave 1
A123 Administrator Dave 2
Q012 Supervisor Jane2
Q012 Administrator Kent3
Select *
From contacts
I want to have from the contacts table the following
ClientName Supervisor Administrator
A123 Dave1 Dave2
2
Answers
Create two new columns Supervisor and Administrator and run two queries, one to update supervisors like
and second to update admin
If you want just data retrieval, without table structure change a simple conditional aggregation will do.
For the given data examples
Query
Result
If you want to change the table structure. Use a single transaction to create a temporary table store the data in the temporary table , then change the structure and insert the data back to main table.
I have taken in consideration that you are using InnoDB storage engine.
See example