-1

Following is the output received from the SQL and I want to convert same into the desired result in SQL. Questions are populated from in question master so questions can change in future. I am stuck since long time. I am using Pivot for the first time.

Please open the link to see the SQL output and desired result

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Assuming Sql Server, this is a simple application of a PIVOT, as follows:

SELECT *
FROM
QuestionsAndAnswers
PIVOT
(
  Min(Answers)
  FOR Question IN ([Question 1], [Question 2], [Question 3])
) pvt;

You will need to take an opinion of an aggregate to be applied to the answers in the pivot, in the event of more than one answer for the same company - I've used Min.

SqlFiddle here.

Note that if you have a variable set of questions, that you'll need to employ a dynamic approach to the pivot, such as this here

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285