Click here to Skip to main content
14,387,927 members
Rate this:
Please Sign up or sign in to vote.
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.

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 22:18pm
v2
Comments
Maciej Los 24-Dec-18 3:14am
   
What kind of output do you want to achieve?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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

Try this:
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;
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100