Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to use Pivot in mysql.But failed to achieve, can anyone help me out. Below is my table structure.

ID	QID	Value
1	10	abc@abc.com
1	11	1234567890
2	10	abc@abc.com
2	11	1234567890
3	10	abc@abc.com
3	11	1234567890
4	10	abc@abc.com
4	11	1234567890
6	10	abc@abc.com
6	11	1234567890
7	10	abc@abc.com
7	11	1234567890
8	10	abc@abc.com
8	11	1234567890
9	10	abc@abc.com
9	11	1234567890
11	10	abc@abc.com
11	11	1234567890


I tried to achieve this by using following line of code, but this is not giving me actual output which is expected.

SQL
Select FEEDBACKID,
max(case when QuestionId=10 then Option_Value  when QuestionId=11 then Option_Value end ) AS Email,
max(case when QuestionId=10 then Option_Value  when QuestionId=11 then Option_Value end ) AS Contact
FROM
PivotTable p

GROUP BY
FEEDBACKID ;


What I have tried:

Hi ,

I have tried many ways to achieve this but failed to achieve the output.
So please help me out .

Thanks in advance.
Posted
Updated 23-Dec-18 21:18pm
v2
Comments
Maciej Los 24-Dec-18 3:14am    
What kind of output do you want to achieve?

1 solution

I'd strongly recommend to read this: MySQL - Pivot table basics: rows to columns[^]

Try this:
SQL
SELECT 
  FEEDBACKID, 
  GROUP_CONCAT(if(QuestionId= 10, Option_Value, NULL)) AS 'Email',
  GROUP_CONCAT(if(QuestionId = 11, Option_Value, NULL)) AS 'Contact'
FROM tbl
GROUP BY FEEDBACKID;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900