I critically need help right now as my project cannot be compledted because of this issue. This is my problem:
I have a table "survey". Each survey has 47 questions which users will answer and store into database. i have mistakenly store each 47 questions into 47 rows. Which means that if 5 people take the survey, i will have total number of 47*5(rows) in my table. so imagine i have 10,000 surveys i will then have 47*10,000(rows). This has affected the performance of my application and report.
Now want i want to achieve now is to transpose each 47rows into 47(columns). FOr example 5*47(rows) will give me 5(rows) 47(columns)
UserID | Q1 | Q2 | Q3 | Q4 |--- |Q47
-----------------------------------------------------
user 1 | Answer1 | answer2 |answer3 |answer 4 | ----answer47
user 2 | Answer1 | answer2 |answer3 |answer 4 | ----answer47
user 3 | Answer1 | answer2 |answer3 |answer 4 | ----answer47
This is my code. The code only transpose the data and groupped by userID, but i want to display all the multiple userID with the corresponding answers. PLEASE HELP ME
$sql = "SELECT agentId,
MAX(IF(questionid= 1, answer, '')) Q1,
MAX(IF(questionid= 2, answer, '')) Q2,
MAX(IF(questionid= 3, answer, '')) Q3,
MAX(IF(questionid= 4, answer, '')) Q4,
MAX(IF(questionid= 43, answer, '')) Q43,
MAX(IF(questionid= 44, answer, '')) Q44,
MAX(IF(questionid= 35, answer, '')) Q45,
MAX(IF(questionid= 46, answer, '')) Q46,
MAX(IF(questionid= 47, answer, '')) Q47,
FROM surveyanswers users GROUP BY agentId ORDER BY agentId";