I have data under this schema in my table (AVG_TEAM field is AVG(AVG_OK) where TEAM_ID = TEAM_ID in line and MONTH = MONTH in line)
DRIVER_ID | MONTH | TEAM_ID | AVG_OK | AVG_TEAM
----------------------------------------------------
005 201901 XXXX 81 84
005 201902 XXXX 84 82
005 201903 XXXX 81 80
005 201904 ZZZZ 84 75
070 201901 RRRR 77 80
070 201902 RRRR 80 80
etc
Some people would like to follow each driver's evolution throughout the monthes. Expected schema would be :
DRIVER_ID | TEAM_ID | AVG_OK_MONTH1 | AVG_OK_MONTH2 | AVG_OK_MONTH3 | ... | AVG_OK_MONTH12 | GLOBAL_AVG | TEAM_GLOBAL_AVG
005 XXXX 81 84 81 ... NULL 82 ?
070 RRRR 77 80 NULL ... NULL 78.5 ?
etc
As you very likely saw, there is already a big flaw in this approach as the TEAM_ID might change with the month, and therefore the AVG_TEAM field cannot be used to easily compute the TEAM_GLOBAL_AVG field
But let's say we put this flaw away, and consider the TEAM_ID wont change. I cannot see an easy approach to convert the first schema to the other, weither with SQL or with PHP (never done that).
I have some very ugly solutions in mind using arrays in PHP but there has got to be better and simpler ways ? Any insight is welcomed.